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\n",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\n");
38     else printf("Yes\n");
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 }

 

 
        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM