SZTU Monthly 2024 Mar.
写在前面
题目难度:
这次比赛的题目都为蓝桥杯往年省赛的原题,难度中等以及中等偏下,不做具体细分。
题目难度都不大,但是由于蓝桥杯的赛制,应该尽量保证一次 \(AC\)
A 移动距离
求出两点的坐标再求距离即可
需要注意的是在奇数行需要翻转
AC Code
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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
int w, m, n;
cin >> w >> m >> n;
m--, n--;
int x = m % w, xx = n % w, y = m / w, yy = n / w;
if (y % 2)
x = w - 1 - x;
if (yy % 2)
xx = w - 1 - xx;
cout << abs(x - xx) + abs(y - yy);
}
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
B 等差数列
可以将数列排序成单调不减数列有:
任意两项之差都为公差 \(d\) 的倍数,故公差 \(d\) 为所有相邻两项差的最大公约数
AC Code
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void solve()
{
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
sort(v.begin(), v.end());
int d = v[1] - v[0];
for (int i = 1; i < n; i++)
d = gcd(d, v[i] - v[i - 1]);
if (d == 0)
cout << n;
else
cout << (v.back() - v.front()) / d + 1;
}
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
C 解码
检查每个字母后面有没有数字,有就按题目要求输出
AC Code
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
string s;
cin >> s;
for (int i = 0; i < s.size(); i++)
{
if (!isdigit(s[i]))
{
int num = 0;
if (i + 1 < s.size() && isdigit(s[i + 1]))
num = s[i + 1] - '0';
for (int k = 0; k < num; k++)
cout << s[i];
if (num == 0)
cout << s[i];
}
}
}
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
D 饮料换购
经典问题,按题目模拟即可
AC Code
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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
int n;
cin >> n;
int ans = 0;
while (n >= 3)
{
ans += n / 3 * 3;
n = n / 3 + n % 3;
}
cout << ans + n << endl;
}
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
E 全球变暖
\(BFS\) 每块岛屿,判断岛上是否存在一格满足上下左右都是 #
,存在则岛屿不会淹没。
AC Code
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
vector<string> mp;
bool vis[N][N];
int n;
bool bfs(int x, int y)
{
bool flag = true;
queue<pair<int, int>> q;
vis[x][y] = true;
q.push({x, y});
while (!q.empty())
{
auto [x, y] = q.front();
q.pop();
int cnt = 0;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= n || mp[nx][ny] == '.')
continue;
if (mp[nx][ny] == '#')
cnt++;
if (vis[nx][ny])
continue;
vis[nx][ny] = true;
q.push({nx, ny});
}
if (cnt == 4)
flag = false;
}
return flag;
}
void solve()
{
cin >> n;
mp.resize(n);
for (int i = 0; i < n; i++)
cin >> mp[i];
int id = 0, ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (mp[i][j] == '#' && !vis[i][j])
ans += bfs(i, j);
}
}
cout << ans;
}
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
F 时间显示
直接取余到时分秒即可
AC Code
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;
typedef long long ll;
void solve()
{
ll n;
scanf("%lld", &n);
n %= 1LL * 24 * 60 * 60 * 1000;
int h = n / (60 * 60 * 1000);
n %= 60 * 60 * 1000;
int m = n / (60 * 1000);
n %= 60 * 1000;
int s = n / 1000;
printf("%02d:%02d:%02d", h, m, s);
}
int main()
{
// cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
G 修剪灌木
走到一端再走回来就是爱丽丝离开当前灌木最长的时间
AC Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cout << max(n - i, i - 1) * 2 << '\n';
}
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
H 乘法表
处理进制,用数组存表即可
AC Code
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int p;
char mp[36];
string to_p(int x)
{
string res;
while (x)
{
res += mp[x % p];
x /= p;
}
reverse(res.begin(), res.end());
return res;
}
void solve()
{
cin >> p;
for (int i = 0; i < 10; i++)
mp[i] = '0' + i;
for (int i = 10; i < 36; i++)
mp[i] = 'A' + i - 10;
for (int i = 1; i < p; i++)
{
for (int j = 1; j <= i; j++)
cout << to_p(i) << '*' << to_p(j) << '=' << to_p(i * j) << ' ';
cout << '\n';
}
}
int main()
{
cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
SZTU Monthly 2024 Mar.
http://xiaowhang.github.io/archives/950106332/