1023. Have Fun with Numbers (20)

题目连接:https://www.patest.cn/contests/pat-a-practise/1023原题如下:

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:
1234567899
Sample Output:
Yes
2469135798

这道题不难,但要注意数据类型过大得用数组存储,核心就是模拟乘2
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char src[25];
 6     int dst[25];
 7     int cnt1[25]={0},cnt2[25]={0};
 8     scanf("%s",&src);
 9     int i,j,w,k=0,flag=1,t=0,tmp;
10     //printf("%d
",strlen(src));
11     for (i=strlen(src)-1;i>=0;i--)
12     {
13         tmp=src[i]-'0';
14         cnt1[tmp]++; //数组1加
15         j=2*tmp+k;   //加倍
16         w=j%10;      //余数
17         k=j/10;      //进位数
18         dst[i]=w;    //记录余数
19         cnt2[w]++;  //数组2加
20     }
21     if (k!=0){
22         t=strlen(src);
23         dst[t]=k;cnt2[k]++;flag=0;}
24     //else t=i-1;
25 
26     if (flag)
27     {
28         for (i=0;i<10;i++)
29         {
30             if (cnt1[i]!=cnt2[i])
31             {
32                 flag=0;
33                 break;
34             }
35         }
36     }
37     if (flag==0)printf("No
");
38     else printf("Yes
");
39 
40     if (t==strlen(src))printf("%d",dst[t]);
41     for (t=0;t<strlen(src);t++)printf("%d",dst[t]);
42     return 0;
43 }


原文地址:https://www.cnblogs.com/wuxiaotianC/p/6368310.html