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
AC代码:
//数字模拟 #include<stdio.h> #include<string.h> int digits1[10]; //统计原数中数字(0-9)出现的次数 int digits2[10]; //统计两倍后中数字(0-9)出现的次数 char str[30]; //将数字以字符串形式读取 int s[30]; //存储各个位上的数字,从高位到低位存储 int main() { //freopen("in.txt","r",stdin); memset(digits1,0,sizeof(digits1)); //初始化 memset(digits2,0,sizeof(digits2)); gets(str); int len = strlen(str); int i; for(i=1; i<=len; i++) { s[i]= str[i-1] - '0'; //存储各个位上的数字,从高位到低位存储,s[0]空出来防止最高位有进位 digits1[s[i]]++; //统计原数中数字(0-9)出现的次数 } int c, d=0; for(i=len; i>0; i--) { //计算翻倍后的数 c = 2*s[i] + d; //注意要加上进位d s[i] = c % 10; d = c / 10; } s[0] = d; //将最高位进位赋值给s[0] if(s[0]==0) { for(i=1; i<=len; i++) digits2[s[i]]++; //统计原数中数字(0-9)出现的次数 int flag = 1; for(i=0; i<10; i++) { if(digits1[i]!=digits2[i]) { flag = 0; break; } } if(flag) //若数字(0-9)出现的次数相同,则匹配 printf("Yes\n"); else printf("No\n"); for(i=1; i<=len; i++) printf("%d",s[i]); printf("\n"); } else { //若多出一位,明显不匹配 printf("No\n"); for(i=0; i<=len; i++) printf("%d",s[i]); printf("\n"); } return 0; }