PassThePAT


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

Book

发表于 2022-08-19

1006 换个格式输出整数

输入234,输出BBSSS1234。

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
#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
scanf("%d", &n);
int index = 0, ans[4];
while (n != 0) {
ans[index++] = n % 10;
n /= 10;
}
for (int i = index - 1; i >= 0; i--) {
if (i == 2) {
for (int j = 0; j < ans[i]; j++) {
printf("B");
}
} else if (i == 1) {
for (int j = 0; j < ans[i]; j++) {
printf("S");
}
} else if (i == 0) {
for (int j = 1; j <= ans[i]; j++) {
printf("%d", j);
}
}
}

return 0;
}

234
BBSSS1234

1021 个位数统计

统计一个不超过1000位的数字的各个数字出现次数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;

int main() {
char str[1100];
cin >> str;
int len = strlen(str);
int cnt[10] = {0};
for (int i = 0; i < len; i++) {
cnt[str[i] - '0']++;
}
for (int i = 0; i < 10; i++) {
if (cnt[i] != 0) {
printf("%d:%d\n", i, cnt[i]);
}
}

return 0;
}

100311
0:2
1:3
3:1

1031 查验身份证

1
2
3
4
5
6
7
8



4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

PAT FOR ALL

发表于 2022-07-11

#ifdef ONLINE_JUDGE

#else
freopen(“C:\Users\DD\Desktop\input.txt”, “r”, stdin);
freopen(“C:\Users\DD\Desktop\output.txt”,”w”,stdout);

#endif

// #ifdef ONLINE_JUDGE
// #else
// freopen(“/home/master/MOVE/doc/Documents/C/inandout/in.txt”, “r”, stdin);
// freopen(“/home/master/MOVE/doc/Documents/C/inandout/out.txt”, “w”, stdout);
// #endif

阅读全文 »

Chapter 1:入门模拟

发表于 2020-10-09

简单模拟

B1001 害死人不偿命的(3n+1)猜想 (15分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;

int main() {
int n, step = 0;
scanf("%d", &n);
while (n != 1) {
if (n % 2 == 0) n = n / 2;
else {
n = (n * 3 + 1) / 2;
}
step++;
}
printf("%d\n", step);

return 0;
}

3
5

B1011 A+B 和 C (15分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

int main() {
int N, kase = 1;
scanf("%d", &N);
while(N--) {
long long a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
if (a + b > c) {
printf("Case #%d: true\n", kase++);
}
else {
printf("Case #%d: false\n", kase++);
}
}

return 0;
}

B1016 部分A+B (15分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;

long long f(long long a, long long da) {
long long pa = 0;
while (a != 0) {
if (a % 10 == da) pa = pa * 10 + da;
a /= 10;
}
return pa;
}

int main() {
long long a, b, da, db, pa = 0, pb = 0;
scanf("%lld%lld%lld%lld", &a, &da, &b, &db);
pa = f(a, da);
pb = f(b, db);
printf("%lld\n", pa + pb);

return 0;
}

B1026 程序运行时间 (15分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

int main() {
int c1, c2;
scanf("%d%d", &c1, &c2);
int res = c2 - c1;
if (res % 100 >= 50) {
res = res / 100 + 1;
}
else {
res = res / 100;
}
printf("%02d:%02d:%02d\n", res / 3600, res % 3600 / 60, res % 60);

return 0;
}

B1046 划拳 (15分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;

int main() {
int T, drinkA = 0, drinkB = 0;
scanf("%d", &T);
while (T--) {
int a1, a2, b1, b2;
scanf("%d%d%d%d", &a1, &a2, &b1, &b2);
if (a2 == a1 + b1 && b2 != a1 + b1) drinkB++;
else if (a2 != a1 + b1 && b2 == a1 + b1) drinkA++;
}
printf("%d %d\n", drinkA, drinkB);

return 0;
}

B1008 数组元素循环右移问题 (20分)

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
#include <bits/stdc++.h>
using namespace std;

int main() {
int num[110] = {0};
int n, m, cnt = 0;
scanf("%d%d", &n, &m);
m = m % n;
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
}
for (int i = n - m; i < n; i++) {
printf("%d", num[i]);
cnt++;
if (cnt < n) printf(" ");
}
for (int i = 0; i < n - m; i++) {
printf("%d", num[i]);
cnt++;
if (cnt < n) printf(" ");
}

return 0;
}

6 2
1 2 3 4 5 6

5 6 1 2 3 4

B1012 数字分类 (20分)

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
#include <bits/stdc++.h>
using namespace std;

int main() {
int N, temp;
int res[5] = {0}, cnt[5] = {0};
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &temp);
if (temp % 5 == 0) {
if (temp % 2 == 0) {
res[0] += temp;
cnt[0]++;
}
}
else if (temp % 5 == 1) {
if (cnt[1] % 2 == 0) {
res[1] += temp;
}
else {
res[1] -= temp;
}
cnt[1]++;
}
else if (temp % 5 == 2) {
cnt[2]++;
}
else if (temp % 5 == 3) {
res[3] += temp;
cnt[3]++;
}
else {
if (temp > res[4]) {
res[4] = temp;
}
cnt[4]++;
}
}
if (cnt[0] == 0) printf("N ");
else printf("%d ", res[0]);
if (cnt[1] == 0) printf("N ");
else printf("%d ", res[1]);
if (cnt[2] == 0) printf("N ");
else printf("%d ", cnt[2]);
if (cnt[3] == 0) printf("N ");
else printf("%.1f ", (double)res[3] / cnt[3]);
if (cnt[4] == 0) printf("N");
else printf("%d", res[4]);


return 0;
}

B1018 锤子剪刀布 (20分)

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
#include <bits/stdc++.h>
using namespace std;

int change(char c) {
if (c == 'B') return 0;
if (c == 'C') return 1;
if (c == 'J') return 2;
}

int main() {
string bcd = "BCJ";
int N;
cin >> N;
int time_A[3] = {0}, time_B[3] = {0};
int hand_A[3] = {0}, hand_B[3] = {0};
char cA, cB;
int nA, nB;
for (int i = 0; i < N; i++) {
cin >> cA >> cB;
nA = change(cA);
nB = change(cB);
if ((nA + 1) % 3 == nB) {
time_A[0]++;
time_B[2]++;
hand_A[nA]++;
}
else if ((nB + 1) % 3 == nA) {
time_B[0]++;
time_A[2]++;
hand_B[nB]++;
}
else if (nA == nB){
time_A[1]++;
time_B[1]++;
}
}
printf("%d %d %d\n", time_A[0], time_A[1], time_A[2]);
printf("%d %d %d\n", time_B[0], time_B[1], time_B[2]);
int winMostA = 0, winMostB = 0;
for (int i = 0; i <= 2; i++) {
if (hand_A[i] > hand_A[winMostA]) winMostA = i;
if (hand_B[i] > hand_B[winMostB]) winMostB = i;
}
printf("%c %c\n", bcd[winMostA], bcd[winMostB]);


return 0;
}

1155 Heap Paths

发表于 2020-06-01

1154 Vertex Coloring

发表于 2020-06-01

1153 Decode Registration Card of PAT

发表于 2020-06-01

1152 Google Recruitment

发表于 2020-06-01

1151 LCA in a Binary Tree

发表于 2020-06-01

1150 Travelling Salesman Problem

发表于 2020-06-01

1149 Dangerous Goods Packaging

发表于 2020-06-01
12…16

e5

158 日志
5 标签
© 2022 e5
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4