比赛链接:https://ac.nowcoder.com/acm/contest/8997
A 完全数
这道题一一遍历即可,但是不能从1-n,会超时,应该从1-sqrt(n)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll n;
ll sum = 1;
cin >> n;
// for(n = 1;n<=1000;++n){
for(int i = 2;i<=sqrt(n);++i){
if(n % i == 0){
sum+=i;
if(i != n / i){
sum += n / i;
}
if(sum > n){
break;
}
}
}
if(sum == n){
cout << "Pure" << endl;
}else if(sum > n){
cout << "Late" << endl;
}else{
cout << "Early" << endl;
}
// }
return 0;
}
B 移动撤销
先进行字符串预处理,再操作
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int x = 0,y = 0;
string s;
int n;
cin >> n;
cin >> s;
int i = 0;
while(i < s.size()){
int j = i;
while(j < s.size() && s[j] == 'Z'){
s[j] = 0;
++j;
}
if(i == j){
++i;
continue;
}
int c = j-i;
i = j;
j -= c;
--j;
while(c && j >= 0){
if(s[j] == 0){
--j;
}else{
s[j--] = 0;
--c;
}
}
}
for(i = 0;i<s.size();++i){
if(s[i] == 'W'){
++y;
}else if(s[i] == 'A'){
--x;
}else if(s[i] == 'S'){
--y;
}else if(s[i] == 'D'){
++x;
}
}
cout << x << ' ' << y << endl;
return 0;
}
C 石头剪刀布
贪心
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll n;
cin >> n;
ll a,b,c,d,e,f;
cin >> a >> b >> c >> d >> e >> f;
ll res = 0;
//石头VS剪刀
int tmp = min(a,e);
res += tmp * 2;
a -= tmp;
e -= tmp;
//剪刀 VS 布
tmp = min(b,f);
res += tmp * 2;
b -= tmp;
f -= tmp;
//布VS石头
tmp = min(c,d);
res += tmp * 2;
c -= tmp;
d -= tmp;
//平局
res += min(a,d);
res += min(b,e);
res += min(c,f);
cout << res << endl;
return 0;
}
D 夹缝中求和
二分查找
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll nums[100010];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
memset(nums,0,sizeof(nums));
ll n,x,y;
cin >> n >> x >> y;
for(int i = 1;i<=n;++i){
cin >> nums[i];
}
sort(nums+1,nums+1+n);
int i = 1;
ll res = 0;
for(;i<=n-1;++i){
ll mi = x - nums[i];
ll ma = y - nums[i];
ll* a = upper_bound(nums + i + 1,nums + n + 1,mi - 1);
ll* b = lower_bound(nums + i + 1,nums + n + 1,ma + 1);
res += b - a;
}
cout << res << endl;
return 0;
}