A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 — are quasibinary and numbers 2, 12, 900 are not.
You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.Input
The first line contains a single integer n (1 ≤ n ≤ 106).Output
In the first line print a single integer k — the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.
In the second line print k numbers — the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn’t matter. If there are multiple possible representations, you are allowed to print any of them.ExamplesinputCopy
9
outputCopy
9 1 1 1 1 1 1 1 1 1
inputCopy
32
outputCopy
3 10 11 11
题意:给数字n,让其被最少的二进制数以十进制方式相加。
思路:按进制进行每位思考,考虑到其中某个位数上最大的数就是构造中相加的1最多的数。通过两次循环把每次大于1的数都抠出来,组成一位输出,直到该数为0.
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6+100;
typedef long long LL;
LL a[15]={0,1,10,100,1000,10000,100000,1000000};
LL b[maxn],n;
///
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n;
LL cnt=0;
LL k=n;
LL maxn=0;
while(k)
{
maxn=max(maxn,k%10);
b[++cnt]=k%10;
k/=10;
}
cout<<maxn<<endl;
for(LL i=1;i<=maxn;i++)
{
LL res=0;
for(LL j=1;j<=cnt;j++)
{
if(b[j]) b[j]--,res+=a[j];
}
cout<<res<<" ";
}
return 0;
}