B. Quasi Binary

98 篇文章 3 订阅

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;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值