codeforces 538 B.Quasi Binary(思维题)

B. Quasi Binary
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
9
output
9
1 1 1 1 1 1 1 1 1 
input
32
output
3
10 11 11 

题目链接:http://codeforces.com/problemset/problem/538/B


题目大意:输入一个整数m,求相加和为m的多个只包含1、0的整数,要求这样整数个数最少。


解题思路:用字符数组输入,再转换为数组,每个数字大于1时减1并输出1,等于0时输出0。注意避免输出前导0。


代码如下:

#include <cstdio>
#include <cstring>
char s[8];
int a[8],b[8];
int main()
{
	int i,j,n,k,f=0,cnt=0;
	scanf("%s",s);
	n=strlen(s);
	for(i=0;i<n;i++)
		b[i]=a[i]=s[i]-'0';
	for(j=0;j<n;j++)
	{
		if(b[j]==0)
			continue;
		while(b[j]>0)
		{
			for(k=j;k<n;k++)
			{
				if(b[k]>0)
					b[k]-=1;
			}
			cnt++;
		}
	}
	printf("%d\n",cnt);
	for(j=0;j<n;j++)
	{
		if(a[j]==0)
			continue;
		while(a[j]>0)
		{
			if(f)
			printf(" ");
			f=1;
			for(k=j;k<n;k++)
			{
				if(a[k]>0)
				{
					a[k]-=1;
					printf("1");
				}
				else
					printf("0");
			}
			cnt++;
		}
	}
	return 0;
}


你可能感兴趣的:(codeforces)