码迷,mamicode.com
首页 > 其他好文 > 详细

[CF538B] Quasi Binary

时间:2020-03-28 20:25:08      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:for   输出   ssi   倒序输出   +=   lin   and   ret   bin   

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 nn . Represent it as a sum of minimum number of quasibinary numbers.

Input

The first line contains a single integer \(n\) \((1<=n<=10^{6})\).

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 Input1

9

Sample Output1

9
1 1 1 1 1 1 1 1 1 

Sample Input2

32

Sample Output2

3
10 11 11 

题解

题意(来自洛谷)

题目描述:给出一个数n,你需要将n写成若干个数的和,其中每个数的十进制表示中仅包含0和1。问最少需要多少个数
输入格式:一行 一个数 n(\(1≤n≤10^6\)
输出格式:最少的数的个数,并给出一种方案。

很显然,最少需要多少个数取决于\(max\)(一个数的每一位),下面举个例子:

\(12321\),组成这个数的最大的数字是\(3\),因此最少\(3\)个满足条件的数就可以构成\(12321\)

分别为\(11111\)\(1110\)\(100\)

这样我们可以将每一位分别处理,这里我是从个位开始处理的

其实这也可以这样理解,\(12321\)是由\(2321\)转移过来的,同理,…

#include<iostream>
#include<cstdio>
using namespace std;

int Ans,num[100];//其实这里定义10就够了,因为最多9个数就一定可以构成n

int main()
{
	int n,Res;
	scanf("%d",&n);
	for(int bit=1;bit<=n;bit*=10)
	{
		Res=(n/bit)%10;//每一位的数值
		Ans=max(Ans,Res);
		for(int i=1;i<=Res;++i) num[i]+=bit;
	}
	printf("%d\n",Ans);
	for(;Ans;--Ans) printf("%d ",num[Ans]);//倒序输出,从小到大
	putchar(‘\n‘);
	return 0;
}

[CF538B] Quasi Binary

标签:for   输出   ssi   倒序输出   +=   lin   and   ret   bin   

原文地址:https://www.cnblogs.com/hihocoder/p/12587269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!