Codeforces Round #640 (Div. 4) A--D

DA. Sum of Round Numbers

链接:Problem - A - Codeforces

题目:

题目样例:

 题目思路:

对于每一个数字,求出每一个位上数字,然后乘10的j次方。

AC:

#include<bits/stdc++.h>
using namespace std;
int t;
int main(){
    cin>>t;
    while(t--){
        string n;
        int a[15][20]={0};
        cin>>n;
        int len=n.size();
        int sum=0;
        int x=0;
        for(int i=len-1;i>=0;i--){
            if(n[i]!='0'){
                sum++;
                a[n[i]-'0'][x]=1;
            }
            x++;
        }
        int f=0;
        cout<<sum<<endl;
        for(int i=1;i<=9;i++){
            for(int j=0;j<=x;j++){
                if(a[i][j]==1){
                    cout<<i*(pow(10,j));
                    f++;
                    if(f!=sum)cout<<" ";
                }
            }
        }
        cout<<endl; 
    }
    return 0;
}

B.Same Parity Summands

链接:Problem - 1352B - Codeforces

题目:

题目样例:

题目思路:

根据题意,需要构造同奇同偶的数组,另前m-1个数字都为1,看剩下的数字是否为奇数且大于0;

前m-1个数字都为2是同理的;

 AC:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
int t,n,m;
int main(){
	cin>>t;
	while(t--){
		cin>>n>>m;
        int x=n-(m-1);
        int y=n-(m-1)*2;
        if(x%2!=0&&x>0){
            cout<<"YES"<<endl;
            for(int i=0;i<m-1;i++){
                cout<<1<<" ";
            }
            cout<<x<<endl;
        }else if(y%2==0&&y>0){
            cout<<"YES"<<endl;
            for(int i=0;i<m-1;i++){
                cout<<2<<" ";
            }
            cout<<y<<endl;
        }else{
            cout<<"NO"<<endl;
        }
	}
	return 0;
}

C. K-th Not Divisible by n

链接:Problem - C - Codeforces

题目:

 题目样例:

 题目思路:

1.那么我们现在就可以算出不能被n整除的前k个数中有多少个组(多少个满编组,如 只有 11 而没有 12 不叫满编)。 k/(n-1)

2.算出来后,我们再算有多少个非满编组。 k%(n-1)

3.再判断非满编组是否为0,如果为0,那么我们在看做一组有n个数的时候其最后一个数是能被整除的,要减去最后一个数,否则要加上非满编组中的元素个数。

AC:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
int main(){
    int t,n,k;
    cin>>t;
    while(t--){
        cin>>n>>k;
        int x=n-1;
        int y=k/(n-1);
        k-=y*x;
        if(k==0)k--;
        cout<<n*y+k<<endl;
    }
    return 0;
}

D. Alice, Bob and Candies

题目链接:https://codeforces.com/contest/1352/problem/D

题目:

 

题目样例:

 

题目思路:

用双指针去模拟,大于上一步的直接加,小于等于上一步再去跑while去加。

AC:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
int t,n,m,a[N];
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        int l=1,r=n;
        int ans1=0,ans2=0,sum=0;
        ans1+=a[1];
        sum++;
        l++;
        int x=a[1];
        while(l<=r){
            if(a[r]>x){
                x=a[r];
                ans2+=a[r];
                r--;
            }else{
                int y=a[r];
                r--;
                while(y<=x&&r>=l){
                    y+=a[r];
                    r--;
                }
                x=y;
                ans2+=y;
            }
            sum++;
            if(l>r)break;
            if(a[l]>x){
                x=a[l];
                ans1+=a[l];
                l++;
            }else{
                int y=a[l];
                l++;
                while(y<=x&&l<=r){
                    y+=a[l];
                    l++;
                }
                x=y;
                ans1+=y;
            }
            sum++;
        }
        cout<<sum<<" "<<ans1<<" "<<ans2<<endl;
    }


    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值