1023. 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 file 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:1234567899Sample Output:
Yes 2469135798
#include <stdio.h> #include <string.h> char str[25]; int num[10], ans[25], num1[10]; int main() { int i = 0, tmp, j; while(scanf("%s", str) != EOF) { memset(num, 0, sizeof(num)); memset(ans, 0, sizeof(ans)); memset(num1, 0, sizeof(num1)); int len = strlen(str); for(i = 0; i < len; i++) num[str[i] - '0']++; for(i = len - 1, j = 0; i >= 0; i--, j++) { int tmp = str[i] - '0'; tmp = 2 * tmp; ans[j] = tmp; } for(i = 0; i < j; i++) { if(ans[i] >= 10) { ans[i] = ans[i] % 10; ans[i+1]++; } } if(ans[i] == 0) i--; for(j = 0; j <= i; j++) { num[ans[j]]--; } for(i = 0; i < 10; i++) if(num[i] != 0) break; if(i < 10) printf("No\n"); else { printf("Yes\n"); } for(i = j - 1; i >= 0; i--) printf("%d", ans[i]); printf("\n"); } return 0; }