# 官方题解|欢乐赛#44
2025-04-09 22:11:34
发布于:浙江
官方题解|欢乐赛#44
T1
思路分析
由于数字 中有两个O
,显然在 中,的O
最多的,所以直接输出 即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << "88";
return 0;
}
T2
思路分析
本题首先要读懂题,摸牌的条件:后面出牌的点数是前面记录的约数,那么就能摸牌,注意第一次出牌的时候不能摸牌,所以只要从后往前扫描下整个数组,满足a[i - 1] % a[i] == 0
答案就加 , 最后统计一下答案即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], n;
int main(){
cin >> n;
int ans = 0;
for(int i = 1; i <= n; i ++ ){
cin >> a[i];
if(i >= 2 && a[i - 1] % a[i] == 0) ans ++;
}
cout << ans;
return 0;
}
T3
思路分析
由于第一次成功的提交不计算罚时,所以每道题的罚时为 ,对于每一个人统计下每道题目的罚时总和即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N];
int n, m;
int main(){
cin >> n >> m;
for(int i = 1;i <= n; i ++ ){
int sum = 0;
for(int j = 1; j <= m; j ++ ){
int x;
cin >> x;
sum += x - 1;
}
sum *= 20;
cout << sum << " ";
}
return 0;
}
T4
思路分析
首先本题需要要读入一个带空格的字符串,然后对于这个字符串进行遍历,依次循环替换成 A
到 Z
即可,可以利用 控制替换的字母,其中是你替换的次数。
代码
#include <bits/stdc++.h>
using namespace std;
string s;
int main(){
getline(cin, s);
char now = 'A';
int cnt = 0;
for(auto &it : s){
if(it == ' '){
it = (char)(now + cnt);
cnt = (cnt + 1) % 26;
}
}
cout << s;
}
T5
思路分析
本题很显然当区间长度是奇数的时候,无法满足条件。当区间长度为偶数的时候,只有整个数组中 和 的出现次数都至少要是 才能满足条件,其中 是区间长度。
代码
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt1, cnt0;//cnt1代表原数组中1的个数,cnt0代表原数组中-1的个数
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i ++ ){
int x;
cin >> x;
if(x == -1) cnt0 ++;
else cnt1 ++;
}
while(m -- ){//处理m个询问
int l, r;
cin >> l >> r;
int len = r - l + 1;//求原数组长度
if((r - l + 1) & 1) cout << "sad\n";
else{
if(cnt0 >= len / 2 && cnt1 >= len / 2) cout << "happy\n";
else cout << "sad\n";
}
}
return 0;
}
T6
思路分析
本题可以用结构体存储数组中原来的值、转化为 进制数的值和转化为 进制数以后的最低位的值,然后分别求出这些信息,按照题目要求进行结构体排序。转八进制的思路可以参考转二进制的思路:将原来的数不断的对 取模,最后记得翻转一下。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
struct Node{
int val;
int last;
string Octal;
}a[N];
int n;
int main(){
cin >> n;
for(int i = 1; i <= n; i ++ ){
cin >> a[i].val;
a[i].last = a[i].val % 8;
int x = a[i].val;
string tmp;
while(x){
tmp = tmp + to_string(x % 8);
x /= 8;
}
reverse(tmp.begin(), tmp.end());
a[i].Octal = tmp;
}
sort(a + 1, a + 1 + n, [&](Node A, Node B){
if(A.last != B.last) return A.last > B.last;
return A.val < B.val;
});
for(int i = 1; i <= n; i ++ ){
cout << a[i].Octal << ' ';
}
return 0;
}
全部评论 1
沙发
2025-04-13 来自 北京
0
有帮助,赞一个