语法周赛 Round 25 题解
2024年12月25日大约 2 分钟
A. WYH 问题目名叫啥
分析
- 难度:简单三位数数位分解,基础条件判断或者字符运算
- 子任务 1(30 分):假设数位分解后三个数为分别是
abc
,那么输出必然是3*w
,星号的部分为b
对应的大写字母。可以用‘A’+b
或者十个if
语句完成。 - 子任务 2(30 分):留个不怕累的同学的分数,根据个位写是个
if
语句即可。 - 子任务 3(40 分):数位分解后依次算出每一位对应的字符即可。
满分参考代码
#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
cin >> n;
int a = n / 100 % 10;
int b = n / 10 % 10;
int c = n % 10;
char aa = 'A' + a;
char cc = 'z' - c;
cout << aa << b << cc;
return 0;
}
B. 惠子相梁
分析
- 难度:简单计算题,输入范围很小,也可以手动计算打表。
满分参考代码
#include <bits/stdc++.h>
using namespace std;
long long a;
long long x, y, temp;
int main()
{
cin >> a;
x = y = 0;
// a^a
temp = 1;
for (int i = 1; i <= a; i++)
temp *= a;
x += temp;
// a!
temp = 1;
for (int i = 1; i <= a; i++)
temp *= i;
x += temp;
// 2*a
x += 2 * a;
// a^2+a
y = a * a + a;
// output
cout << x - y;
return 0;
}
C. 文件 IO
分析
- 难度:基础字符串输入输出
按题意输入一个字符串,输出特定内容即可。主要转义符用法,常用的转义符有:
\
:'\\'
'
:'\''
"
:'\"'
满分参考代码
#include <bits/stdc++.h>
using namespace std;
string wotojo;
int main()
{
cin >> wotojo;
cout << "freopen(\"" << wotojo << ".in\", \"r\", stdin);\n";
cout << "freopen(\"" << wotojo << ".out\", \"w\", stdout);\n";
return 0;
}
D. 26.59 s
分析
- 难度:基础二维数组枚举及位置关系判断
- 子任务 1(30 分):只有一个格子,答案必然是
(如果是雷,就没有地方放数字了。如果不是雷就没有数字了) - 子任务 2(30 分):根据那一个雷所处的位置,可以确定周边有几个数字。四个角上有三个数字,边上有五个数字,中间有八个数字。
- 子任务 3(40 分):枚举二维数组每个位置,检查周边有没有雷即可。
满分参考代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
char g[105][105];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
int cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (g[i][j] == '*')
continue;
bool flag = false;
for (int x = i - 1; x <= i + 1; x++)
for (int y = j - 1; y <= j + 1; y++)
if (g[x][y] == '*')
flag = true;
cnt += flag;
}
cout << cnt;
return 0;
}