1. 程式人生 > >PTA-1023 ——Have Fun with Numbers(部分正確)

PTA-1023 ——Have Fun with Numbers(部分正確)

是否 進行 lang 不能 name code 為什麽 方法 notice

題目:

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

分析:

數字翻倍後是否是原數字的排列。我的想法是:翻倍後數字進行排序,依次比較是否一樣。但這種方法不能全部正確,不知道為什麽(其它方法是數數字個數)。

代碼:

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5 string s;
 6 int x[21];
 7 int cmp[21];    //復制一份原本的數字
 8 int ans[21];    //翻倍後的結果 
 9 bool judge=true;    //是否為正數 
10 int main(){
11     cin>>s;
12     if(s[0]==-){        //考慮出現負數的情況 
13         judge=false;
14         s=s.erase(0,1);
15     } 
16     if(s=="0"){
17         cout<<"Yes"<<endl<<"0";
18     }else{
19         for(int i=0;i<s.length();i++){        //翻倍逆序存放 
20             x[s.length()-1-i]=(s[i]-0)*2;
21             cmp[s.length()-1-i]=s[i]-0; 
22         }
23         for(int i=0;i<s.length();i++){        //逢10進位 
24             if(x[i]>=10){
25                 x[i+1]+=1;
26                 x[i]-=10;
27             }
28         }
29         for(int i=0;i<21;i++){    //x之後會被排序打亂掉,提前備份好打亂前的數字 
30             ans[i]=x[i];
31         }
32         int l=0;
33         bool flag=false;
34         for(int i=20;i>=0;i--){        //記錄翻倍後位數 
35             if(x[i]!=0||flag){
36                 l++;
37                 flag=true;    
38             }
39         }
40         if(l==s.length()){        //排序後比較是否一樣 
41             sort(cmp,cmp+l);
42             sort(x,x+l);
43             bool yes=true;
44             for(int i=0;i<l;i++){
45                 if(cmp[i]!=x[i]){
46                     yes=false;
47                     break;
48                 }
49             }
50             if(yes){
51                 cout<<"Yes"<<endl;
52                 if(!judge){
53                     cout<<"-";
54                 }
55                 for(int i=l-1;i>=0;i--){
56                     cout<<ans[i];
57                 } 
58             }else{
59                 cout<<"No";
60             }
61         }else{
62             cout<<"No";
63         }
64     }
65     return 0;
66 } 

 

PTA-1023 ——Have Fun with Numbers(部分正確)