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 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

tip:该题既然只是两倍,那就当作大数相加的逻辑进行运算,然后正常写一套即可

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     char a[25],b[25],c[25];
 5     int cnta[260]={0},cntc[260]={0};
 6     bool flag=true;
 7     cin>>a;
 8     strcpy(b,a);
 9     memset(c,0,sizeof(c));
10     for (int i=strlen(a)-1;i>=0;--i){
11         c[i]=(a[i]-'0'+b[i]-'0')%10+'0';
12         if (i-1>=0){
13             b[i-1]+=(a[i]-'0'+b[i]-'0')/10;
14         }
15         cnta[a[i]]++;
16         cntc[c[i]]++;
17     }
18     if (a[0]<'5'){
19         for (int i=0;i<=256;++i){
20             if (cnta[i]!=cntc[i]){
21                 flag=false;
22             }
23         }
24     }
25     else
26         flag=false;
27     
28     if (flag)
29         cout<<"Yes"<<endl;
30     else
31         cout<<"No"<<endl;
32     if (a[0]>='5'){
33         cout<<'1';
34     }
35     cout<<c<<endl;
36 }

 

posted on 2022-06-02 16:57  Coder何  阅读(33)  评论(0)    收藏  举报