PAT_A1023#Have Fun with Numbers

Source:

PAT A1023 Have Fun with Numbers (20 分)

Description:

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

Keys:

Code:

 1 /*
 2 Data: 2019-07-11 20:38:07
 3 Problem: PAT_A1023#Have Fun with Numbers
 4 AC: 16:20
 5 
 6 题目大意:
 7 给定K位数,翻倍后,是否为原数的重排列
 8 */
 9 #include<cstdio>
10 #include<string>
11 #include<algorithm>
12 #include<iostream>
13 using namespace std;
14 
15 string Double(string s)
16 {
17     string ans;
18     int carry=0,len=s.size();
19     for(int i=0; i<len; i++)
20     {
21         int temp = (s[len-i-1]-'0')*2+carry;
22         ans.insert(ans.end(), temp%10+'0');
23         carry = temp/10;
24     }
25     while(carry != 0)
26     {
27         ans.insert(ans.end(), carry%10+'0');
28         carry /= 10;
29     }
30     reverse(ans.begin(),ans.end());
31     return ans;
32 }
33 
34 int main()
35 {
36     string s;
37     cin >> s;
38     string t = Double(s);
39     string p = t;
40     sort(s.begin(),s.end());
41     sort(t.begin(),t.end());
42     if(s == t)  printf("Yes\n");
43     else        printf("No\n");
44     cout << p;
45 
46 
47     return 0;
48 }

 

posted @ 2019-07-11 20:41  林東雨  阅读(229)  评论(0编辑  收藏  举报