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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值