# 官方题解|欢乐赛#39
2025-01-22 17:19:06
发布于:浙江
官方题解|欢乐赛#39
T1
把题目中的式子转化为代码即可,对应的是取余。
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << 114514 + 1919810 % 114514;
return 0;
}
T2
直接for循环模拟,如果是采用的pow
,记得要强制转化为整数类型哦,pow
默认是浮点数的。
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N], n, m, k;
int main(){
int x; cin >> x;
int ans = 1;
for(int i = 1; i <= x; i ++ ) ans *= 2;
cout << ans;
return 0;
}
T3
按照题目要求模拟即可,可以开一个bool数组f
,f[i]
表示下标为i
的位置上是否有棋子,最后对f
数组求和即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n, m;
bool f[N];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i ++ ){
int x;
cin >> x;
if(f[x]) f[x + 1] = 1;
else f[x] = 1;
}
cout << accumulate(f + 1, f + 1 + m, 0) << endl;
return 0;
}
T4
可以先确定大小,假设从大到小分别为 ,那么我们可以对 同时进行操作 ,直到把 加到和 一样大。然后对 不断的进行操作 ,直到 ,如果最后 比 和 都大 ,那么最后还需要对 都加上 。
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N], n, m, k;
void solve(){
int a, b, c;
//先把啊a, b, c从小到大处理好
vector<int>q;
for(int i = 1; i <= 3; i ++ ){
int x;
cin >> x;
q.push_back(x);
}
sort(q.begin(), q.end());
a = q[0], b = q[1], c = q[2];
//把b和c变的一样大
int cha = c - b;
int ans = cha;
a += cha; b += cha;
//把a不断+2,知道a>=b
int shang = (c - a) / 2;
a += shang * 2;
ans += shang;
if(a != c) ans += 2; //这是a最后等于c+1的情况。
cout << ans << endl;
}
int main(){
int tt = 1;
cin >> tt;
while(tt -- ){
solve();
}
return 0;
}
T5
本题先要预处理出前缀最大值,可以先开一个数组 , 表示 ~ 里面的最大值。最后只要看当前山的高度是否比前面的最高的山的高度高即可,如果满足题目要求那么这座山上就能看到海。
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N], n, m, k;
int maxn[N];
int main(){
cin >> n;
for(int i = 1; i <= n; i ++ ){
cin >> a[i];
if(i == 1) maxn[i] = a[i];
else{
maxn[i] = max(maxn[i - 1], a[i]);
}
}
int ans = 1;
for(int i = 2; i <= n; i ++ ){
if(a[i] >= maxn[i]) ans ++;
}
cout << ans << endl;
return 0;
}
T6
本题考查分类讨论,只要把小王能赢得情况全部列举出来即可,具体的看代码哦。
#include <bits/stdc++.h>
using namespace std;
void solve(){
int a, b, c, d;
cin >> a >> b >> c >> d;
//ac bd
int cnt = 0, ans = 0;
if(a >= c && b > d) cnt += 2;
else if(a > c && b >= d) cnt += 2;
if(a >= d && b > c) cnt += 2;
else if(b >= c && a > d) cnt += 2;
cout << cnt << endl;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
int tt = 1;
cin >> tt;
while(tt -- ){
solve();
}
return 0;
}
这里空空如也
有帮助,赞一个