- 西中经开联校 - 第 2 场周赛
【题解】西中经开联校 - 第 2 场周赛
- 2024-12-16 10:34:04 @
T1 A+B=B
难度:需要掌握分支以及字符和数字之间的转换。
分做法:由于数据中 的数据能保证 为 ,所以直接输出 即可。
满分做法:由于数据比较小,只有 ,所以计算结果会在 ,转换为字符是 。将两个字符转换为数字相加,再判断结果是否小于 ,小于 就将结果直接换为字符;否则将十位数和个位数分别算出来,输出两个字符。
C++ 代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char a, b;
cin >> a >> b;
int ans = (a - 'A') + (b - 'A');
if (ans < 10) cout << (char)(ans + 'A') << endl;
else
{
int g = ans % 10; // 个位
int s = ans / 10; // 十位
cout << (char)(s + 'A') << (char)(g + 'A') << endl;
}
return 0;
}
T2 ABBA
难度:直接使用循环解决。
满分做法: 的 次方,用循环计算 次乘以 的结果; 的 次方,用循环计算 次乘以 的结果,变量记得开 long long
(最后计算出来的数据量会超出 int
范围)。计算出结果后直接判断即可(用到了 else if
,或者只用 if
语句也可以)。
C++ 代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
long long ab = 1, ba = 1;
for (int i = 1; i <= b; i ++ ) ab *= a;
for (int i = 1; i <= a; i ++ ) ba *= b;
if (ab > ba) cout << "first" << endl;
else if (ab < ba) cout << "second" << endl;
else cout << "same" << endl;
return 0;
}
T3 环状字符串
难度:能正确使用字符串即可。
分做法:数据的开始和结束在 之间,直接输出字符串中的第 个字符到 个字符,。字符串是从 开始的。
满分做法:循环从第 个字符到 个字符,如果 大于字符串长度,以字符串环状的规律来看,要输出的是 i % len
的字符。
C++ 代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
int n, m;
cin >> s >> n >> m;
for (int i = n - 1; i <= n + m - 2; i ++ )
cout << s[i % s.size()];
return 0;
}
T4 照明
难度:需要熟练掌握二维数组以及嵌套循环的使用。
满分做法:首先在二维数组中找出灯所在的位置,并在此位置进行操作,将上下左右都照亮并标记,但是根据题目要求,一旦碰到墙就停止,所以四个方向上都要进行判断,碰到墙或者到达二维数组的边缘就停下来。最后在二维数组中判断有多少点被标记了。
C++ 代码:
#include <bits/stdc++.h>
using namespace std;
int n;
char g[35][35];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main()
{
cin >> n;
memset(g, '#', sizeof g);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
cin >> g[i][j];
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
if (g[i][j] == '*')
{
for (int k = 0; k < 4; k ++ )
{
int x = i, y = j;
while (g[x][y] != '#')
{
if (g[x][y] == '.')
g[x][y] = 'o';
x += dx[k], y += dy[k];
}
}
}
int ans = 0;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
if (g[i][j] == 'o' || g[i][j] == '*')
ans ++ ;
cout << ans << endl;
return 0;
}
0 comments
No comments so far...