数据结构-起步能力自测题 自测-4 Have Fun with Numbers(20 分)

时间:2021-06-26 17:03:25

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
 

 
 
最大位数超过long long范围,用字符串存输入数字。然后,转换成数组,并算出2倍数组。最后,计算数字出现次数,比较得出结论。 
   
 1 #include <stdio.h>
 2 int main()
 3 {
 4     char p[21];
 5     int a1[20], a2[21];    /*换算数组,a1原来,a2二倍(考虑进位,多一)*/
 6     int i, j;
 7 
 8     scanf("%s", p);
 9 
10     /*调试*/
11 //    printf("%s\n", p);
12 
13     int n = 0;    /*位数*/
14     for (i = 0; i < 20; i++)
15         if (p[i] != '\0')
16         {
17             a1[i] = p[i] - '0';    /*字符转数字*/
18             a2[i] = a1[i] * 2;    /*乘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     /*调试*/
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 };    /*计算数字出现次数*/
46     int t2[10] = { 0,0,0,0,0,0,0,0,0,0 };
47 
48     /*调试*/
49     //for (i = 0; i <10; i++)
50     //    printf("%d ", t1[i]);
51     //printf("\n");
52 
53     if (a2[0] > 9)    /*多1位,No。位数相同,继续*/
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++)    /*比较*/
68             if (t1[i] == t2[i])
69                 continue;
70             else
71             {
72                 printf("No\n");
73                 break;
74             }
75         if (i == 10)    /*全相同*/
76             printf("Yes\n");
77     }
78 
79     /*调试*/
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 }