PAT(Basic Level) Practice 题解(C/C++)
2021-02-13 18:42:07
本文总阅读量

因为要准备复试,之前的学长推荐刷一下PAT乙级的题目,做了几道发现乙级都是些简单的模拟题,注意一下特殊情况即可,难度不大。

1001 害死人不偿命的(3n+1)猜想
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main() {
int n, cnt = 0;
cin >> n;
while (n != 1) {
if (n & 1) n = (3 * n + 1) / 2;
else n /= 2;
cnt++;
}
cout << cnt << endl;
return 0;
}
1002 写出这个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

string a[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

int main() {
string s;
cin >> s;
int sum = 0;
for (auto v : s) {
sum += v - '0';
}
string t = to_string(sum);
for (int i = 0; i < t.size(); i++) {
if (i != 0) cout << " ";
cout << a[t[i] - '0'];
}
return 0;
}
1006 换个格式输出整数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main() {
int n;
cin >> n;
int a, b, c; // 分别代表百位,十位,个位
a = n / 100;
b = (n / 10) % 10;
c = n % 10;
while (a--) cout << 'B';
while (b--) cout << 'S';
for (int i = 1; i <= c; i++) cout << i;
return 0;
}
1007 素数对猜想

题目说的比较绕,说白了就是找两个同时不超过N的相邻且相差2的素数。例如:3与5,5与7,11与13……

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
#include <iostream>

using namespace std;

const int N = 100010;

bool prime[N];

void checkPrime() {
for (int i = 2; i < N; i++) prime[i] = true;
for (int i = 2; i < N; i++) {
bool isPrime = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (!isPrime) prime[i] = false;
}
}

int main() {
checkPrime();
int n, cnt = 0;
cin >> n;
for (int i = 2; i + 2 <= n; i++) {
if (prime[i] && prime[i + 2] && !prime[i + 1]) {
cnt++;
}
}
cout << cnt << endl;
return 0;
}
1008 数组元素循环右移问题

考研王道书上面有类似思路的题,也就是翻转三次。

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
#include <iostream>

using namespace std;

const int N = 110;

int a[N];
int n, m;

void reverse(int x, int y) { // 将数组a中下标从x到y的部分翻转
for (int i = x, j = y; i < j; i++, j--) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}

int main() {
cin >> n >> m;
m = m % n;
for (int i = 1; i <= n; i++) cin >> a[i];
reverse(1, n);
reverse(1, m);
reverse(m + 1, n);
for (int i = 1; i <= n; i++) {
if (i > 1) cout << " ";
cout << a[i];
}
return 0;
}
1009 说反话
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
vector<string> v;
string s;
while (cin >> s) {
v.push_back(s);
}
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i];
if (i != 0) {
cout << " ";
}
}
return 0;
}
1011 A+B和C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdio>

using namespace std;

typedef long long LL;

int main() {

int n;
cin >> n;
for (int i = 1; i <= n; i++) {
LL a, b, c;
cin >> a >> b >> c;
printf("Case #%d: ", i);
if (a + b > c) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
}
return 0;
}
1013 像素数
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
#include <iostream>
#include <cstdio>

using namespace std;

void findAndPrintPrime(int a, int b) {
bool flag = true;
int cnt = 0;
int t = 0;
for (int i = 2; flag; i++) {
bool isPrime = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) isPrime = false;
}
if (isPrime) {
cnt++;
if (cnt >= a && cnt <= b) {
t++;
if (t == 1) cout << i;
else cout << " " << i;
if (t == 10) {
cout << endl;
t = 0;
}
}
if (cnt == b) flag = false;
}
}
}

int main() {
int a, b;
cin >> a >> b;
findAndPrintPrime(a, b);
return 0;
}
1021 个位数统计
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

int a[10];

int main() {
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
a[s[i] - '0']++;
}
for (int i = 0; i < 10; i++) {
if (a[i] > 0) {
cout << i << ":" << a[i] << endl;
}
}
return 0;
}
1022 D进制的A+B

十进制转化为k进制就是除k取余,再将所得余数倒序输出。手算10进制转2进制也是这么做的。

要注意的是本题的测试点3需要特判a+b为0的情况。

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
#include <iostream> 
#include <vector>

using namespace std;

void printKary(int sum, int k) {
vector<int> v;
while (sum) {
int d = sum % k;
v.push_back(d);
sum /= k;
}
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i];
}
}

int main() {
int a, b, d, sum;
cin >> a >> b >> d;
sum = a + b;
if (sum == 0) cout << 0 << endl;
else printKary(sum, d);
return 0;
}
1023 组个最小数
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
#include <iostream>

using namespace std;

int a[10];

int main() {
for (int i = 0; i < 10; i++) cin >> a[i];
for (int i = 1; i < 10; i++) { // 输出第一个最小的不为0的数字
if (a[i] != 0) {
cout << i;
a[i]--;
break;
}
}
while (a[0]) { // 输出0
cout << 0;
a[0]--;
}
for (int i = 1; i < 10; i++) { // 从小到大输出
while (a[i] != 0) {
cout << i;
a[i]--;
}
}
return 0;
}
1032 挖掘机技术哪家强
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
#include <iostream>

using namespace std;

const int N = 100010;

int school[N];

int main() {
int n;
int idx, score;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> idx >> score;
school[idx] += score;
}
int maxIdx = 0, maxScore = -1;
for (int i = 1; i <= n; i++) {
if (school[i] >= maxScore) {
maxIdx = i;
maxScore = school[i];
}
}
cout << maxIdx << " " << maxScore << endl;
return 0;
}
1036 跟奥巴马一起编程
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
#include <iostream>
#include <cmath>

using namespace std;

int main() {
int n;
char c;
cin >> n >> c;

for (int i = 0; i < n; i++) cout << c;

int m = round(n / 2.0) - 2; // round函数返回四舍五入的结果,需要头文件cmath
for (int i = 0; i < m; i++) {
cout << endl;
for (int j = 0; j < n; j++) {
if (j == 0 || j == n - 1) cout << c;
else cout << " ";
}
}

cout << endl;
for (int i = 0; i < n; i++) cout << c;
cout << endl;

return 0;
}
1037 在霍格沃兹找零钱
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 <iostream>
#include <algorithm>

using namespace std;

int conversion(int a, int b, int c) { // 转化成一个数
int res = a * 17 * 29 + b * 29 + c;
return res;
}

int main() {
int num1[3], num2[3];
char c;
cin >> num1[0] >> c >> num1[1] >> c >> num1[2];
cin >> num2[0] >> c >> num2[1] >> c >> num2[2];
int a = conversion(num1[0], num1[1], num1[2]), b = conversion(num2[0], num2[1], num2[2]);
if (b < a) { // 若b小于a,则计算a-b,并在前面加负号,与我们所学的运算习惯相符。
cout << '-';
swap(a, b);
}
int dif = b - a;
cout << dif / 29 / 17 << '.' << dif / 29 % 17 << '.' << dif % 29 << endl;
return 0;
}
1038 统计同成绩学生
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int score[110];

int main() {
int n, m, t;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t;
score[t]++;
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> t;
if (i > 0) cout << ' ';
cout << score[t];
}
return 0;
}
1050 螺旋矩阵
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
#include <bits/stdc++.h>

using namespace std;

const int N = 10010;

int x;
int p[N];
int n, m;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
cin >> x;
int y = sqrt(x);
if (y * y == x) n = y, m = y;
else {
while (y * y < x) y++;
while (x % y != 0) y++;
n = y, m = x / n;
}
vector<vector<int>> ans(n, vector<int>(m));
vector<vector<bool>> vis(n, vector<bool>(m));
for (int i = 0; i < x; i++) cin >> p[i];
sort(p, p + x);
reverse(p, p + x);
int a = 0, b = 0, cnt = 0, k = 0;
while (cnt < x) {
ans[a][b] = p[cnt];
vis[a][b] = true;
cnt++;
int xx = a + dx[k], yy = b + dy[k];
if (xx < 0 || xx >= n || yy < 0 || yy >= m || vis[xx][yy]) k = (k + 1) % 4;
a = a + dx[k], b = b + dy[k];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j == 0) cout << ans[i][j];
else cout << " " << ans[i][j];
}
cout << endl;
}
return 0;
}
1056 组合数的和
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;

const int N = 15;

int n, ans;
int a[N];

int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
ans += a[i] * 10 + a[j];
}
}
cout << ans << endl;
return 0;
}
1059 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
47
48
49
50
#include <bits/stdc++.h>

using namespace std;

const int N = 10010;

map<string, int> mp;
set<string> st;
int n, k;
string t;

bool check(int x) {
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return false;
}
return true;
}

int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> t;
mp[t] = i;
}
cin >> k;
for (int i = 1; i <= k; i++) {
cin >> t;
cout << t << ": ";
if (mp.count(t) == 0) {
cout << "Are you kidding?" << endl;
continue;
}
if (st.count(t) != 0) {
cout << "Checked" << endl;
continue;
}
st.insert(t);
int idx = mp[t];
if (idx == 1) {
cout << "Mystery Award" << endl;
} else {
if (check(idx)) {
cout << "Minion" << endl;
} else {
cout << "Chocolate" << endl;
}
}
}
return 0;
}
1060 爱丁顿数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int n, ans;
int a[N];

int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
if (a[i] > n - i + 1) {
ans = n - i + 1;
break;
}
}
cout << ans << endl;
return 0;
}
1061 判断题
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
#include <iostream>

using namespace std;

const int N = 110;

int score[N]; // 记录每一题分数
int correct[N]; // 记录每一题正确答案
int n, m;

int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> score[i];
for (int i = 1; i <= m; i++) cin >> correct[i];
while (n--) {
int ans = 0, t;
for (int i = 1; i <= m; i++) {
cin >> t;
if (t == correct[i]) {
ans += score[i];
}
}
cout << ans << endl;
}
return 0;
}
1062 最简分数

需要注意测试点1为:给定的两个分数中,前面的比后面的大。

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
#include <iostream>
#include <algorithm>

using namespace std;

int gcd(int a, int b) { // 返回a与b的最大公约数
return b ? gcd(b, a % b) : a;
}

int main() {
int a, b, c, d, k;
char ch;
cin >> a >> ch >> b >> c >> ch >> d >> k;
if ((double)a / b > (double)c / d) { // 题干并没有说前面的分数小于后面的分数,所以为了方便起见保证:前<后
swap(a, c);
swap(b, d);
}
bool first = true; // 用于卡输出格式
for (int i = 1; i < k; i++) {
if (a * k >= b * i || i * d >= k * c) continue; // 如果该分数不在给定的两个分数之间
if (gcd(i, k) == 1) { // 若是最简分式
if (first) {
cout << i << '/' << k;
first = false;
} else {
cout << ' ' << i << '/' << k;
}
}
}
return 0;
}
1063 计算谱半径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
int n, a, b;
double ans = 0.0, t;
cin >> n;
while (n--) {
cin >> a >> b;
t = sqrt(a * a + b * b);
if (t > ans) ans = t;
}
printf("%.2f", ans);
return 0;
}
1064 朋友数
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
#include <iostream>

using namespace std;

// 证号最小1(1),最大36(9999)
bool st[40];

int getCertificate(int x) {
int res = 0;
while (x) {
int d = x % 10;
res += d;
x /= 10;
}
return res;
}

int main() {
int n, t, cnt = 0;
cin >> n;
while (n--) {
cin >> t;
int res = getCertificate(t);
if (!st[res]) {
st[res] = true;
cnt++;
}
}
cout << cnt << endl;
bool first = true;
for (int i = 1; i <= 36; i++) {
if (st[i]) {
if (first) {
cout << i;
first = false;
} else {
cout << " " << i;
}
}
}
return 0;
}
1065 单身狗
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
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <algorithm>

using namespace std;

int n, m;
map<string, string> mp; // 用于记录伴侣
set<string> st; // 用于将派对中的伴侣删除,以便计算落单的客人
string a, b, t;

int main() {
cin >> n;
while (n--) {
cin >> a >> b;
mp[a] = b, mp[b] = a;
}
cin >> m;
while (m--) {
cin >> t;
if (st.count(mp[t]) != 0) {
st.erase(mp[t]);
} else {
st.insert(t);
}
}
cout << st.size() << endl;
bool first = true;
for (auto it = st.begin(); it != st.end(); it++) {
if (first) {
cout << *it;
first = false;
} else {
cout << ' ' << *it;
}
}
return 0;
}
1066 图像过滤

关于测试点3的超时问题,这个题居然卡cin,用scanf就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdio>

using namespace std;

int n, m, a, b, k;
int t;

int main() {
cin >> n >> m >> a >> b >> k;
while (n--) {
for (int i = 0; i < m; i++) {
scanf("%d", &t); // 这里用cin会超时
if (t >= a && t <= b) t = k;
// 输出
if (i == 0) printf("%03d", t);
else printf(" %03d", t);
}
cout << endl;
}
return 0;
}
1067 试密码
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
#include <iostream>
#include <string>

using namespace std;

string s, pw;
int n, cnt;

int main() {
cin >> s >> n;
getline(cin, pw); // 将第一行的回车读掉。
while (true) {
getline(cin, pw);
if (pw == "#") break;
cnt++;
if (pw == s) {
cout << "Welcome in" << endl;
break;
} else {
cout << "Wrong password: " << pw << endl;
if (cnt == n) {
cout << "Account locked" << endl;
break;
}
}
}
return 0;
}
1068 万绿丛中一点红

这个题坑点:①:要考虑边界。②:正常人都是x代表行,y代表列,不知道这个题为啥要反常规😂。

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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unordered_map>

using namespace std;

const int N = 1010;

int g[N][N];
int n, m, k;
int cnt;
int a, b; // 记录找到的坐标
unordered_map<int, int> mp; // 用于判断是否唯一

int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; // row的偏移量
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; // col的偏移量

int main() {
scanf("%d%d%d", &m, &n, &k);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &g[i][j]);
mp[g[i][j]]++;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
bool find = true;
for (int u = 0; u < 8; u++) { // 检查与四周的像素差
int xx = i + dx[u], yy = j + dy[u];
if (xx < 1 || xx > n || yy < 1 || yy > m) continue; // 出界
if (abs(g[i][j] - g[xx][yy]) <= k) {
find = false;
break;
}
}
if (find && mp[g[i][j]] == 1) { // 如果与四周的像素差很大并且唯一
cnt++;
if (cnt == 1) {
a = j, b = i;
}
if (cnt == 2) {
goto end; //不建议使用goto
}
}
}
}
end:
if (cnt == 0) {
cout << "Not Exist" << endl;
} else if (cnt == 1) {
printf("(%d, %d): %d\n", a, b, g[b][a]);
} else {
cout << "Not Unique" << endl;
}
return 0;
}
1069 微博转发抽奖
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 <iostream>
#include <set>
#include <string>

using namespace std;

int m, n, s;
string t;
set<string> st;

int main() {
cin >> m >> n >> s;
if (s > m) { // 如果没有人抽奖直接按题干输出
cout << "Keep going..." << endl;
return 0;
}
for (int i = 1; i <= s - 1; i++) cin >> t; // 跳过前面不中奖的人
int cnt = 0;
for (int i = s; i <= m; i++) {
cin >> t;
if (st.count(t) != 0) continue; // 如果之前抽到过,直接continue
if (cnt % n == 0) { // n个一循环
cout << t << endl;
st.insert(t); // 用set记录中间名单
}
cnt++;
}
return 0;
}
1086 就不告诉你
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {
int a, b, ans;
cin >> a >> b;
ans = a * b;
string s = to_string(ans);
reverse(s.begin(), s.end());
int i = 0;
while (s[i] == '0') i++; // 跳过前导零
for (;i < s.size(); i++) cout << s[i];
return 0;
}
1070 结绳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

int n;
int a[N];
int res;

int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n); // 贪心的思想,尽量让短的绳子折叠的次数多,这样总的长度就最长
res = a[1]; // 所以只需按绳子长度从小到大排序,从前往后折即可
for (int i = 2; i <= n; i++) {
res = (res + a[i]) / 2;
}
cout << res << endl;
return 0;
}
1071 小赌怡情

坑点:输出格式那里是两个空格。。

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
#include <iostream>
#include <cstdio>

using namespace std;

int T, K;
int n1, b, t, n2;

int main() {
cin >> T >> K;
for (int i = 1; i <= K; i++) {
cin >> n1 >> b >> t >> n2;
if (t > T) { // 钱不够
printf("Not enough tokens. Total = %d.\n", T);
continue;
}
if (b == 0 && n2 < n1 || b == 1 && n2 > n1) { // 赢了
T += t;
printf("Win %d! Total = %d.\n", t, T);
} else {
T -= t;
printf("Lose %d. Total = %d.\n", t, T);
if (T == 0) {
printf("Game Over.\n");
return 0;
}
}
}
return 0;
}
1076 Wifi密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int n;
string a, b, c, d;

int main() {
cin >> n;
while (n--) {
cin >> a >> b >> c >> d;
if (a[2] == 'T') cout << a[0] - 'A' + 1;
if (b[2] == 'T') cout << b[0] - 'A' + 1;
if (c[2] == 'T') cout << c[0] - 'A' + 1;
if (d[2] == 'T') cout << d[0] - 'A' + 1;
}
return 0;
}
1082 射击比赛
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 <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int n;
string id;
int x, y;

int minv = 200, maxv = -1;
string minId, maxId;

int main() {
cin >> n;
while (n--) {
cin >> id >> x >> y;
int d = sqrt(x * x + y * y);
if (d > maxv) maxId = id, maxv = d;
if (d < minv) minId = id, minv = d;
}
cout << minId << ' ' << maxId << endl;
return 0;
}
1083 是否存在相等的差
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;

const int N = 10010;

int n, t;
map<int, int, greater<int>> mp;

int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> t;
mp[abs(t - i)]++;
}
for (auto it = mp.begin(); it != mp.end(); it++) {
if (it->second == 1) continue;
cout << it->first << " " << it->second << endl;
}
return 0;
}
1084 外观数独
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
#include <bits/stdc++.h>

using namespace std;

int d, n;
string pre, cur;

int main() {
cin >> d >> n;
pre = pre + to_string(d);
cur = pre; // 防止n为1的情况
for (int i = 2; i <= n; i++) {
cur = "";
for (int l = 0, r = 0; l < pre.size(); l = r) {
r = l;
while (pre[r] == pre[l]) {
r++;
}
cur += pre[l];
cur += to_string(r - l);
}
pre = cur;
}
cout << cur << endl;
return 0;
}
1087 有多少不同的值
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
#include <iostream>

using namespace std;

const int N = 10010;

bool st[N];

int get(int n) {
return n / 2 + n / 3 + n / 5;
}

int main() {
int n, cnt = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int num = get(i);
if (!st[num]) {
st[num] = true;
cnt++;
}
}
cout << cnt << endl;
return 0;
}
1091 N-自守数
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
#include <bits/stdc++.h>

using namespace std;

const int N = 25;

int n;
int a[N];
int k, s;

bool check(int x) {
int t = x, p = 1;
while (t) {
p *= 10;
t /= 10;
}
for (int i = 1; i <= 9; i++) {
if (i * x * x % p == x) {
k = i;
s = i * x * x;
return true;
}
}
return false;
}

int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (check(a[i])) {
cout << k << " " << s << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
1092 最好吃的月饼
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
#include <iostream>

using namespace std;

const int N = 1010;

int n, m, t;
int a[N];

int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> t;
a[j] += t;
}
}
int maxv = -1;
for (int i = 1; i <= n; i++) {
if (a[i] > maxv) maxv = a[i];
}
cout << maxv << endl;
bool first = true;
for (int i = 1; i <= n; i++) {
if (a[i] != maxv) continue;
if (first) {
cout << i;
first = false;
} else {
cout << " " << i;
}
}
return 0;
}
1093 字符串A+B
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>

using namespace std;

char c;
set<char> st;

int main() {
while ((c = getchar()) != EOF) {
if (c == '\n') continue;
if (st.count(c) == 0) {
cout << c;
st.insert(c);
}
}
return 0;
}