2021-05-30

该篇博客介绍了一个C++程序,该程序接受一个正整数n,然后将其表示为最少数量的准二进制数(仅包含0和1的十进制数)之和。程序首先读取输入的十进制数,然后通过迭代转换其每一位,构造出准二进制数,并存储这些数。最后,程序输出最少的准二进制数个数以及这些数的组合,展示了一种有效的数字转换和组合优化方法。
摘要由CSDN通过智能技术生成

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.

Examples
Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11

#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
int a[1011],b[1011];
char c[1010];
int dd(int l)
{
	int s=0;
	for(int i=0; i<l; i++)
	{	
		if(a[i]!=0)
		{
			s=s*10+1;
			a[i]--;
		}
		else
			s=s*10+0;
	}
	return s;
}
int main()
{
	int i,j,k=0,x,n,m,l;
	scanf("%s",c);
	l=strlen(c);
	for(i=0; i<l; i++)
		a[i]=c[i]-'0';
	while((x=dd(l))&&x)
	{
		b[k++]=x;
	}
	sort(b,b+k);
	printf("%d\n",k);
	for(i=0; i<k; i++)
		if(!i)
			printf("%d",b[i]);
		else
			printf(" %d",b[i]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值