1023 Have Fun with Numbers (20 分)

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 kdigits, 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 <bits/stdc++.h>
 2 using namespace std;
 3 int vis[10];
 4 string s;
 5 bool flag = true;
 6 
 7 int main(){
 8     cin >> s;
 9     for(int i = 0; i < s.length(); i++){
10         int x = s[i] - '0';
11         vis[x]++;
12     }
13     int ans = 0;
14     for(int i = s.length()-1; i >= 0; i--){
15         int x = s[i] - '0';
16         int k = x*2 + ans;
17         int an = k%10;
18         s[i] = an + '0';
19         if(vis[an] >= 1)
20             vis[an]--;
21         else{
22             flag = false;
23         }
24         ans = k/10;
25     }
26     if(ans == 1)
27         s = '1'+ s;
28     if(flag)
29         cout <<"Yes"<<endl;
30     else
31         cout <<"No"<<endl;
32     cout <<s<<endl;
33     return 0;
34 }

 

posted @ 2019-06-19 00:58  #忘乎所以#  阅读(463)  评论(0编辑  收藏  举报