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 int main() 3 { 4 char p[ 21 ]; 5 int a1[ 20 ], a2[ 21 ]; /* Convert the array, a1 is the original, a2 is twice (considering the carry, more a) */ 6 int i, j; 7 8 scanf( " %s " , p); 9 10 /* debug */ 11 // printf("%s\n", p); 12 13 int n = 0 ; /* Number of digits */ 14 for (i = 0 ; i < 20 ; i++ ) 15 if (p[i] != ' \0 ' ) 16 { 17 a1[i] = p[i] - ' 0 ' ; /* Character to number */ 18 a2[i] = a1[i] * 2 ; /* multiply by 2 */ 19 n++ ; 20 } 21 else 22 { 23 break ; 24 } 25 26 /*调试*/ 27 //printf("n=%d\n", n); 28 //for (i = 0; i < n; i++) 29 // printf("%d", a2[i]); 30 //printf("\n"); 31 32 for (j = n - 1; j > 0; j--) /*进位*/ 33 for (i = j; i > 0; i--) 34 if (a2[i] > 9) 35 { 36 a2[i] -= 10; 37 a2[i - 1 ] += 1 ; 38 } 39 40 /* Debug */ 41 // for (i = 0; i < n; i++) 42 // printf("%d", a2[i]); 43 // printf ("\n"); 44 45 int t1[ 10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; /* Count the number of occurrences */ 46 int t2[10 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; 47 48 /* Debug */ 49 // for (i = 0; i <10; i++) 50 // printf("%d ", t1[i]); 51 // printf("\n"); 52 53 if (a2[ 0 ] > 9 ) /* 1 more digit, No. Same number of digits, continue */ 54 { 55 printf( "No\n"); 56 } 57 else 58 { 59 for (j = 0; j < 10; j++) 60 for (i = 0; i < n; i++) 61 { 62 if (a1[i] == j) 63 t1[j]++; 64 if (a2[i] == j) 65 t2[j]++; 66 } 67 for (i = 0; i < 10; i++) /* compare */ 68 if (t1[i] == t2[i]) 69 continue ; 70 else 71 { 72 printf( " No\n " ); 73 break ; 74 } 75 if (i == 10 ) /* all the same */ 76 printf( " Yes\n " ); 77 } 78 79 /* Debug */ 80 //for (i = 0; i < 10; i++) 81 // printf("%d ", t1[i]); 82 //printf("\n"); 83 //for (i = 0; i < 10; i++) 84 // printf("%d ", t2[i]); 85 //printf("\n"); 86 87 for (i = 0; i < n; i++) /*第二行输出*/ 88 printf("%d", a2[i]); 89 90 return 0; 91 }