标签:
Description
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.
Sample Input
9
9
1 1 1 1 1 1 1 1 1
32
3
10 11 11
解题思路:题目大意:将一个数拆分成几个由1和0组成的数。输出数的个数以及各个拆分后的数。
当n小于9的时候,只能拆分成n个1;
当n大于9时,用一个数组将n的各个位数存起来,其中最大的那个数Max则为输出数的个数;
输出时,共有Max个循环对数组输出,当数组元素大于等于1时,输出1,同时元素值减一;当数组元素等于0时,输出0;
注意:当数组输出的第一个数为0时选择不输出。直到第一个数不为0时输出。
#include<iostream> #include<stdio.h> #include<cstring> using namespace std; int c[10]; int main() { string s; cin>>s; int sum; if(s.length()==1){ printf("%d\n",s[0]-‘0‘); for(int i=1;i<=s[0]-‘0‘;i++) { if(i==1) printf("1"); else printf(" 1"); } printf("\n"); } else { int k=s.length(); for(int i=1;i<=s.length();i++) c[i]=s[s.length()-i]-‘0‘; int Max=0; for(int i=1;i<=k;i++) { if(c[i]>Max) Max=c[i]; } printf("%d\n",Max); while(Max--) { int i;int j=1; for(i=k;i>=1;i--) { //cout<<i<<"H"<<c[i]<<endl; if(c[i]==0&&j==1) {continue;} else { j++; if(c[i]!=0){printf("1");c[i]=c[i]-1;} else printf("0"); } } printf(" "); } printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/hr974041087/p/5738651.html