Have Fun with Numbers

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

  1 #include<stdio.h>
  2 #include<string.h>
  3 int hash1[10]={0};
  4 int hash2[10]={0};
  5  
  6 int jud(char a[],int len)
  7 {
  8     if(a[0]>='5'&&a[0]<='9')
  9     {
 10         return 0; //false代表不合格的进位大整数; 
 11     }
 12     if(a[0]=='0'&&len!=1)
 13     {
 14         return 2;
 15     }
 16         if(a[0]=='0'&&len==1)
 17     {
 18         return 1;
 19     }
 20     if(a[0]>='1'&&a[0]<'5')
 21     {
 22         return 1;
 23     }
 24 }
 25 int main()
 26 {
 27     char a[25];
 28     int inta[25],i,len,temp;
 29     gets(a);
 30     len=strlen(a);
 31     /*将字符串数组转化为对应整数,存入变换的整数数组中*/
 32     for(i=0;i<len;i++)
 33     {
 34         inta[i]=a[i]-'0';
 35         hash1[inta[i]]++;
 36     }
 37     /*/检测输出
 38         for(i=0;i<len;i++)
 39     {
 40         printf("%d",inta[i]);
 41     }
 42     */
 43     //检测变化数组内容的数字出现次数; 
 44     /**/
 45 //        printf("\n");
 46 //        for(i=0;i<10;i++)
 47 //    {
 48 //        printf("%d ",hash1[i]);
 49 //    }
 50     /**/
 51 //    printf("\n");
 52      int count=0;
 53     for(i=len-1;i>0;i--)
 54     {
 55         temp=inta[i]*2+count;
 56         if(temp>=10)
 57         {
 58             count=1;
 59         }
 60         else{
 61             count=0;
 62         }
 63         inta[i]=temp%10;
 64     }
 65     inta[0]=inta[0]*2+count;
 66     //将变换数组*2
 67  
 68     //判断数组 
 69 if(    jud(a,len)==1)
 70 {
 71  
 72 //对*2后的数组中数字出现的次数进行统计;     
 73         for(i=0;i<len;i++)
 74     {
 75         hash2[inta[i]]++;
 76     }
 77 //        for(i=0;i<10;i++)
 78 //    {
 79 //        printf("%d ",hash2[i]);
 80 //    }
 81     
 82     //比较两hash表是否完全相等
 83     for(i=0;i<10;i++)
 84     {
 85         if(hash1[i]!=hash2[i])
 86         {
 87             printf("No\n");
 88                 for(i=0;i<len;i++)
 89             {
 90                 printf("%d",inta[i]);
 91              }
 92             return 0;
 93         }
 94     }
 95         printf("Yes\n");
 96         for(i=0;i<len;i++)
 97     {
 98         printf("%d",inta[i]);
 99      }
100 }
101  
102 else if(jud(a,len)==2)
103 {        
104         printf("No\n");
105         for(i=0;i<len;i++)
106         {
107             printf("%d",inta[i]);
108         }
109 }
110 else
111 {
112     printf("No\n");
113             for(i=0;i<len;i++)
114         {
115             printf("%d",inta[i]);
116         }
117 }
118     
119     
120     return 0;
121  } 
posted @ 2020-01-11 20:04  一斜星辰酱  阅读(346)  评论(0编辑  收藏  举报