牛客IOI周赛20-普及组 代码

比赛链接: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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值