Codeforces Round #300, problem: (B) Quasi Binary 【贪心+二进制位数上升】

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_42429718/article/details/102666263

Codeforces Round #300, problem: (B) Quasi Binary


题目大意

给定一个数字,让其仅有0,1组成的K个数构成,保证K最小。


题解

贪心

不难发现,把输入的数字想象成字符串,k的最小值就是字符串中数字的最大值,1的个数其实就是字符串每一位的数字 例如32 就是先将个位 a [ 0 ] + = 1 a [ 1 ] + = 1 然后在十位 a [ 0 ] + 10 a [ 1 ] + = 10 a [ 2 ] + = 10 整合起来就是 10 11 11 即为输出 每上升一位我们加的就增加10倍 这是很显然的 然后对于K值 我们只需要对字符串的每一位取最大值即可

#include<bits/stdc++.h>
using namespace std;
const int maxn=100+10;
char s[maxn];
int n;
int kmax=0,k=0;
int a[maxn];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>s;
    n=strlen(s);
    int t=1;
    for(int i=n-1;i>=0;i--){
        k=s[i]-'0';
        kmax=max(kmax,k);
        for(int i=0;i<k;i++){
            a[i]+=t;
        }
        t*=10;
    }
    cout<<kmax<<endl;
    for(int i=kmax-1;i>=1;i--)
        cout<<a[i]<<" ";
    cout<<a[0]<<endl;
    return 0;
}
学如逆水行舟,不进则退

猜你喜欢

转载自blog.csdn.net/weixin_42429718/article/details/102666263