7-2 Have Fun with Numbers (20分)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes 2469135798
代码的讲解,本题主要利用了hash和大数相乘。想明白这俩个就很容易了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 30
int main()
{
char a[maxsize];
int b[maxsize];
int c[10];
int d[10];
int dig=0;
memset(b,0,maxsize*sizeof(int));
memset(c,0,10*sizeof(int));
memset(d,0,10*sizeof(int));
scanf("%s",a);
if(a[0]=='0')
{
printf("Yes\n");
printf("0");
return 1;
}
int i;
int str=strlen(a);
for(i=str-1;i>=0;i--)
{
c[a[i]-'0']++;
b[str-i-1]=(a[i]-'0')*2+dig;
if(b[str-i-1]>9)
{
dig=b[str-i-1]/10;
b[str-i-1]=b[str-i-1]%10;
}
else
{
dig=0;
}
}
if(dig)
{
b[str]=dig;
}
for(i=maxsize-1;i>=0;i--)
{
if(b[i]!=0)
{
break;
}
}
int j;
for(j=i;j>=0;j--)
{
d[b[j]]++;
}
for(j=0;j<10;j++)
{
if(c[j]!=d[j])
break;
}
if(j==10)
{
printf("Yes\n");
}
else
printf("No\n");
for(j=i;j>=0;j--)
printf("%d",b[j]);
printf("\n");
return 0;
}