7-49 Have Fun with Numbers (20分)

7-49 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

这题是一个典型的大数相乘 用了哈希来记录个位数出现次数。

#include<stdio.h>
#include<string.h>
#define N 23
int main()
{
 char s[N];
 int c=0;
 int a[N];
 int b[10];
 int d[10];
 memset(a,0,N*sizeof(int));
 memset(b,0,10*sizeof(int));
 memset(d,0,10*sizeof(int));
 scanf("%s",s);
 int i;
 int len=strlen(s);
 for(i=0;s[i]!='\0';i++)
 a[i]=s[len-i-1]-'0';
 for(i=0;i<len;i++)
 {
  b[a[i]]++;
 }
 for(i=0;i<len;i++)
 {
  a[i]=a[i]*2+c;
  c=a[i]/10;
  a[i]=a[i]%10;
 }
 if(c!=0)
 {
 a[i]+=c;
 len+=1;
    }
 for(i=0;i<len;i++)
  d[a[i]]++;
 int flag=1;
 for(i=0;i<10;i++)
 {
  if(d[i]!=b[i])
  flag=0;
 }
 if(flag)
 {
 printf("Yes\n");
    }
    else
    printf("No\n");
 for(i=len-1;i>=0;i--)
 printf("%d",a[i]);
 printf("\n");
     
 
 
     
 return 0;
 }

posted @ 2020-10-16 22:16  罪梦者  阅读(230)  评论(0)    收藏  举报