PAT-2022年夏季考试-乙级 满分代码
摘要
PAT-2022年夏季考试-乙级 满分代码
Powered by:NEFU AB-IN
PAT-2022年夏季考试-乙级
部分题意之后补全
-
A
-
题意
-
思路
模拟两数相加即可
-
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32'''
Author: NEFU AB-IN
Date: 2022-06-05 13:25:50
FilePath: \Contest\a.py
LastEditTime: 2022-06-05 13:32:45
'''
d = {
"1110011": "3",
"0000011": "1",
"1011111": "0",
"1110110": "2",
"0101011": "4",
"1111001": "5",
"1111101": "6",
"1000011": "7",
"1111111": "8",
"1111011": "9",
}
a = list(map(str, input().split()))
b = list(map(str, input().split()))
for i in range(len(a)):
a[i] = d[a[i]]
for i in range(len(a)):
b[i] = d[b[i]]
numa = "".join(a)
numb = "".join(b)
print(int(numa) + int(numb))
-
-
B
-
题意
-
思路
模拟题目要求即可
-
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79/*
* @Author: NEFU AB-IN
* @Date: 2022-06-05 13:35:07
* @FilePath: \Contest\b.cpp
* @LastEditTime: 2022-06-05 13:52:00
*/
using namespace std;
const int N = 1e6 + 10;
struct sa
{
int id, val, day, order;
int v[8];
} a[N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i].id;
for (int j = 1; j <= 7; ++j)
{
int val;
cin >> val;
if (val)
{
a[i].day += 1;
a[i].val += val;
a[i].v[j] = val;
}
}
}
for (int i = 1; i <= 7; ++i)
{
printf("Star %d\n", i);
int mx = 0;
for (int j = 1; j <= n; ++j)
{
mx = max(mx, a[j].v[i]);
}
if (!mx)
{
printf("NONE\n");
continue;
}
for (int j = 1; j <= n; ++j)
{
if (a[j].v[i] == mx)
{
printf("%06d\n", a[j].id);
}
}
}
printf("Star of the week\n");
auto cmp = [&](sa a, sa b) {
if (a.val != b.val)
return a.val > b.val;
return a.order < b.order;
};
sort(a + 1, a + 1 + n, cmp);
int mx = 0;
for (int i = 1; i <= n; ++i)
{
if (a[i].val >= mx && a[i].day >= 3)
{
mx = a[i].val;
printf("%06d\n", a[i].id);
}
}
return 0;
}
-
-
C
-
题意
-
思路
相约一下两边的阶乘即可
-
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46'''
Author: NEFU AB-IN
Date: 2022-06-05 13:53:51
FilePath: \Contest\c.py
LastEditTime: 2022-06-05 14:04:10
'''
left = 1
right = 1
def f(x, y):
global left, right
mn = min(x, y)
if x == y:
return
if mn == x:
ans = 1
for i in range(x + 1, y + 1):
ans *= i
right *= ans
if mn == y:
ans = 1
for i in range(y + 1, x + 1):
ans *= i
left *= ans
n = int(input())
for _ in range(n):
a, b, c, d = map(int, input().split())
if a > d:
a, d = d, a
if b > c:
b, c = c, b
f(a, b)
f(d, c)
if left == right:
print("YES")
else:
print("NO")
left = right = 1
-
-
D
-
题意
-
思路
题目保证了一个孩子只会说一句真话,所以拿其中一个孩子的话碰另一个孩子的话即可
-
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93'''
Author: NEFU AB-IN
Date: 2022-06-05 14:08:12
FilePath: \Contest\d.py
LastEditTime: 2022-06-05 14:40:31
'''
def dec1(x):
return (x - 1 + 7) % 7
def dec2(x):
return (x - 2 + 7) % 7
def add1(x):
return (x + 1) % 7
def add2(x):
return (x + 2) % 7
def f_y(x):
y = x # 求出虚假的日期
if y == b_y:
return [1, "yesterday"]
if y == dec1(b_t):
return [1, "today"]
if y == dec2(b_to):
return [1, "tomorrow"]
return [0, None]
def f_t(x):
t = x
if t == b_t:
return [1, "today"]
if t == add1(b_y):
return [1, "yesterday"]
if t == dec1(b_to):
return [1, "tomorrow"]
return [0, None]
def f_to(x):
to = x
if to == b_to:
return [1, "tomorrow"]
if to == add1(b_t):
return [1, "today"]
if to == add2(b_y):
return [1, "yesterday"]
return [0, None]
d = {
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday"
}
a_y, a_t, a_to = map(int, input().split())
b_y, b_t, b_to = map(int, input().split())
f = f_y(a_y)
if f[0]:
print(d[add1(a_y)])
print("yesterday")
print(f[1])
f = f_t(a_t)
if f[0]:
print(d[a_t])
print("today")
print(f[1])
f = f_to(a_to)
if f[0]:
print(d[dec1(a_to)])
print("tomorrow")
print(f[1])
# f = f_y(b_y, a_y, a_t, a_to)
# f = f_t(b_t, a_y, a_t, a_to)
# f = f_to(b_to, a_y, a_t, a_to)
-
-
E LRU缓存
-
题意
LRU 全称为 Least Recently Used,即“最近最少使用”。LRU
缓存机制是指,当缓存满了,而缓存区外面的一个新数据被调用的时候,将缓存中最近最少使用(即最长时间没有被使用过)的数据清除,为新数据开辟出空间。
你的任务就是实现这种 LRU 缓存机制。 -
思路
采用双指针,当滑动窗口中元素个数超出范围时,移动左指针,直到滑动窗口中元素种类满足即可,元素有重复的没有关系
-
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62/*
* @Author: NEFU AB-IN
* @Date: 2022-06-05 14:41:59
* @FilePath: \Contest\e.cpp
* @LastEditTime: 2022-06-05 15:35:10
*/
/*
7-5 LRU缓存
分数 25
作者 陈越
单位 浙江大学
LRU 全称为 Least Recently Used,即“最近最少使用”。LRU
缓存机制是指,当缓存满了,而缓存区外面的一个新数据被调用的时候,将缓存中最近最少使用(即最长时间没有被使用过)的数据清除,为新数据开辟出空间。
你的任务就是实现这种 LRU 缓存机制。
*/
using namespace std;
const int N = 2e4 + 10;
int st[N];
int main()
{
int n, m;
cin >> n >> m;
vector<int> a(m + 1);
vector<int> ans;
for (int i = 1; i <= m; ++i)
{
cin >> a[i];
}
int cnt = 0;
for (int i = 1, j = 1; i <= m; ++i)
{
if (st[a[i]] == 0)
cnt += 1;
st[a[i]] += 1;
if (cnt > n)
{
while (j < i)
{
st[a[j]] -= 1;
if (st[a[j]] == 0)
{
ans.push_back(a[j]);
j++;
cnt -= 1;
break;
}
j++;
}
}
}
for (int i = 0; i < SZ(ans); ++i)
{
cout << ans[i] << " "[i == SZ(ans) - 1];
}
}
-