题目
链接
https://www.luogu.com.cn/problem/CF538B
字面描述
题面翻译
题目描述
给出一个数n,你需要将n写成若干个数的和,其中每个数的十进制表示中仅包含0和1。
问最少需要多少个数
输入输出格式
输入格式:
一行 一个数 n(1≤n≤10^6)
输出格式:
最少的数的个数,并给出一种方案。
题目描述
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.
输入格式
The first line contains a single integer $ n $ ( $ 1<=n<=10^{6} $ ).
输出格式
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.
样例 #1
样例输入 #1
9
样例输出 #1
9
1 1 1 1 1 1 1 1 1
样例 #2
样例输入 #2
32
样例输出 #2
3
10 11 11
思路
此问题说将 n n n分解为各个数位只能是 1 1 1或 0 0 0的数的和。
转化一下(翻译成人话):至多少分解成 m a x ( ( n / 1 0 i ) m o d ( 10 ) ) max((n/10^i)mod(10)) max((n/10i)mod(10))(各个数位上数的最大值,最多为 9 9 9),再将这些平均分配落实到每一个数中即可。
例如 114514 114514 114514
根据上述,至少分解成 5 5 5个数,分别是 111111 111111 111111, 1101 1101 1101, 1101 1101 1101, 1101 1101 1101, 100 100 100
代码实现
#include<bits/stdc++.h>
using namespace std;
const int maxn=100+10;
int n,cnt;
int ans[maxn];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i*=10){
int x=(n/i)%10;
cnt=max(cnt,x);
for(int j=1;j<=x;j++)ans[j]+=i;
}
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)printf("%d ",ans[i]);
return 0;
}