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:
1 |
1234567899 |
Sample Output:
1 2 |
Yes 2469135798 |
忘记自己之前是什么时候做的了……
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include<stdio.h> #include<string.h> void reserve(char *str) { int i,n; char temp; n=strlen(str); for(i=0;i<n/2;i++) { temp=str[i]; str[i]=str[n-i-1]; str[n-i-1]=temp; } return ; } int main() { char num[22],result[22],*p; int carry=0,temp,i; gets(num); strcpy(result,num); reserve(result); for(i=0;i<strlen(result);i++) { temp=result[i]-'0'; temp*=2; temp+=carry; carry=temp/10; result[i]=temp%10+'0'; } if(carry>0) { result[i]=carry+'0'; result[i+1]='\0'; reserve(result); printf("No\n%s\n",result); return 0; } reserve(result); for(i=0;i<strlen(result);i++) { if((p=strchr(num,result[i]))!=NULL) { *p=1; } else { printf("No\n%s\n",result); return 0; } } printf("Yes\n%s\n", result); } |