官方题解|欢乐赛#37
2025-01-08 14:42:49
发布于:浙江
官方题解|欢乐赛#37
T1
本题的结果会超过 ,所以要强制类型转化一下,1ll * a * b
,就是把结果转化为 的类型,否则在乘的时候会超过 范围。
#include <iostream>
using namespace std;
int main(){
cout << 1ll*114514*1919810;
return 0;
}
T2
本题考查数位分离,只要利用循环求出数位之和,然后再判断一下奇偶即可。
#include <iostream>
using namespace std;
void solve(){
int n, sum = 0; cin >> n;
while(n){
sum += n % 10; n/=10;
}
if(sum&1) cout << "YES\n";
else cout << "NO\n";
}
int main(){
int t;cin>>t;
while(t --){
solve();
}
return 0;
}
T3
上次考了 ,这次就考下 , , 如何求 ,你枚举也行,辗转相除也行,不卡你时间复杂度。
#include <iostream>
using namespace std;
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
int main(){
int a, b;
cin >> a >> b;
cout << a * b / gcd(a, b);
return 0;
}
T4
本题考查字符串的基本操作,反转一个字符串,你可以循环写,也可以直接调用 STL 里面的 reverse
,最后输出 s + t
即可,t
是s
反转的字符串。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s; cin >> s;
string t = s;
reverse(t.begin(), t.end());
cout << s + t;
return 0;
}
T5
本题首先的思路应该是要求出这个序列中的「中间值」,按照题目要求先排个序,再取下标为 对应的序列的值即可(下标从1开始哦)。然后你要枚举一下,这个里面有多少数是和你求的「中间值」一样的,因为你对于某一个「中间值」进行,它就会变大,跑到序列后面去,整个序列的中间值其实没有改变,所以和原序列「中间值」相等的数都要进行操作才能改变这个序列的「中间值」。
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, a[N];
void solve(){
cin >> n;
for(int i = 1; i <= n; i ++ ) cin >> a[i];
sort(a + 1, a + 1 + n);
int ans = 0;
int idx = (n + 1) / 2;
while(a[idx] == a[(n + 1) / 2] && idx <= n) {
ans ++;
idx ++;
}
cout << ans << endl;
}
int main(){
int t;cin>>t;
while(t --){
solve();
}
return 0;
}
T6
本题考查模拟能力,枚举上下左右的时候注意数组不要越界哈,可以采用方向数组+check
函数的写法,像极了写搜索, 0变成 1,1 变成 0 可以直接异或 1 来运算。
#include <bits/stdc++.h>
using namespace std;
const int N = 5;
int a[N][N], ans[N][N], n, m, k;
int dx[] = {1, -1, 0, 0, 0}, dy[] = {0, 0, 1, -1, 0};
bool check(int x, int y){
return x >= 1 && x <= 3 && y >= 1 && y <= 3;
}
int main(){
for(int i = 1; i <= 3; i ++ )
for(int j = 1; j <= 3; j ++ )
cin >> a[i][j];
for(int i = 1; i <= 3; i ++ )
for(int j = 1; j <= 3; j ++ )
ans[i][j] = 1;
for(int i = 1; i <= 3; i ++ ){
for(int j = 1; j <= 3; j ++ ){
while(a[i][j] -- ){
for(int k = 0; k < 5; k ++ ){
if(check(i + dx[k], j + dy[k])) ans[i + dx[k]][j + dy[k]] ^= 1;
}
}
}
}
for(int i = 1; i <= 3; i ++ ){
for(int j = 1; j <= 3; j ++ ){
cout << ans[i][j];
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个