题目:
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 ≤ 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.
解答:唉,很累!差点因为他放弃算法;其实没多难就是不可能想到这方向上,简述题目
比如:110 101 1 10 11这些十进制数字我们叫他伪二进制,现在给一个十进制数,请计算最小使他成立的二进制数的个数,并且输出大致的二进制数字;
输入 : 32
输出 : 3
11 11 10
贪心思想:比如 8576 ,找最小的 5 连续减去5次 11111;得到3021 找到0;作为限制数;减去1011得到2010;找到两个0;减去1010;得到1000;最后减去1000;
上代码:
#include"stdio.h"
#include"string.h"
char b[1000][10];
int main()
{
char a[10];
while(~scanf("%s",a))
{
int len=strlen(a);
int d=0;
int cc=0;
for(int j=0;j<100;j++)
{
cc=0;
for(int k=0;k<len;k++)
{
if(a[k]=='0')
{
cc++;
b[j][k]='0';
}
else
{
b[j][k]='1';
}
}
for(int p=0;p<len;p++)
{
if(a[p]!='0')
a[p]--;
}
if(cc==len)
break;
d++;
}
printf("%d\n",d);
int t=0;
for(int i=0;i<d;i++)
{
t=0;
for(int j=0;j<len;j++)
{
if(b[i][j]!='0')
{
t=1;
}
if(t==1)
printf("%c",b[i][j]);
}
printf(" ");
}
printf("\n");
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}
代码没啥难度,我就模拟一下大致意思;
比如输入 78902 用字符储存;
7 8 9 0 2存入一维字符数组 并且用循环搜索该数组,遇到0的 在另外一个二维数组中同下标号记住0除了0之外就赋值1
78902 11101
除了0都减一
67801 11101
同上
67800 11100
以次往下;
加油!