欢乐赛#34题解 :) 超基础级语法
2024-11-18 20:12:46
发布于:上海
T1
根据题意要计算出数组共有几个不重复的元素
用set秒了
#include <iostream>
#include <set>
using namespace std;
set<int> si;
int n, x;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> x;
si.insert(x);
}
cout << si.size();
return 0;
}
T2
走路
#include <iostream>
using namespace std;
int n, x, y;
char a;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a;
if(a == 'D') y--;
else if(a == 'U') y++;
else if(a == 'R') x++;
else if(a == 'L') x--;
}
cout << "(" << x << "," << y << ")";
return 0;
}
T3
T3根据T4的提示就是个思维题↓
。。。。好吧(づ′▽`)づ
首先题目要求
求min( |d1-d2|, |d1-d3|, |d2-d3| )的最大值
那么要让三个数的两两之间的差的最小值 最大
就是要让d1, d2, d3之间差越大
稍微举例一下就能发现规律
以下举例用 1 代表工作日,用 x 代表休息日
天数 | 分配 | 答案 |
---|---|---|
10 | 11x1x1111x | 1 |
11 | 111x1x1111x | 1 |
12 | 111x1x11111x | 2 |
13 | 111x1x111111x | 2 |
14 | 1111x1x111111x | 2 |
15 | 1111x1x1111111x | 3 |
看出规律了吗?
把一个工作日夹在剩下两个休息日中间
所以d2 = 1
整个月剩下的天数就是 n - 1(最后一天休息日)- 3 (一个工作日夹在剩下两个休息日中间)
剩下的天数分成 d1, d3
可以想到,当 2d1 = d3 的时候 |d1-d2| 和 |d2-d3| 是最大的
所以d1,d3 分别是 剩余天数(n-4)的 1/3 和 2/3
以及,如果n-4除不尽三就把d1加一
- 以下超短题解
#include <iostream>
using namespace std;
int n, x, y = 1, z;
int main(){
cin >> n;
n -= 4;
if(n % 3 == 0) x = n / 3;
else x = n / 3 + 1;
z = n - x;
cout << min(x - 1, z - x);
return 0;
}
T4
求质数个数,用埃筛
#include <iostream>
using namespace std;
int n, ans;
bool f[1000010];
void prime(){
f[1] = 1;
for(int i = 2; i <= 1000010; i++){
if(f[i] == 0){
for(int j = i * 2; j <= 1000010; j += i) f[j] = 1;
}
}
}
int main(){
cin >> n;
prime();
for(int i = 1; i <= n; i++){
if(f[i] == 0) ans++;
}
cout << ans;
return 0;
}
T5
「好串」:如果这两个字符串中每个字符出现的次数相同(顺序不需要相同)则被称作为一对好串
把字符数组排序,然后逐个比较就行
#include <iostream>
#include <algorithm>
using namespace std;
int t;
int n;
char a[10010], b[10010];
int main(){
cin >> t;
while(t--){
bool f = 1;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i];
}
sort(a, a+n);
sort(b, b+n);
for(int i = 0; i < n; i++){
if(a[i] != b[i]){
f = 0;
break;
}
}
if(f) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
T6
Good Luck!
#include <iostream>
using namespace std;
int main(){
cout << "Good Luck!";
return 0;
}
- 求赞!
这里空空如也
有帮助,赞一个