Have Fun with Numbers

自测-4 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

 

 

 

 

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int main(){
 5     string s1;
 6     cin >> s1;//将数字按字符串的格式读入 
 7     vector<int> myVec; 
 8     vector<int> myVec1;
 9     for(int i = 0; i < s1.size(); i++){
10         myVec.push_back(s1[i] - '0');//依次将字符转化为数字存入vector中 
11         myVec1.push_back(2 * (s1[i] - '0'));//依次将字符转化为数字的二倍存入vector中 
12     }
13     //这里请注意:上面将每一位扩大为原来数字的二倍可能会大于9
14     //所以要将每一位进位处理 
15     //从最后一位开始处理 
16     for(int i = myVec1.size() - 1; i >= 0; i--){
17         if(i != 0){
18             myVec1[i - 1] = myVec1[i] / 10 + myVec1[i - 1];
19         
20             myVec1[i] %= 10;
21                 
22         }else{
23             break;
24         }    
25     
26     } 
27     //分别开辟俩个大小为10的vector
28     //用以记录0-9各数字的个数 
29     vector<int> myVec2(10, 0);
30     
31     vector<int> myVec3(10, 0);
32     //如果上面的进位处理会使总位数增加,也就是第一位会大于9,则输出"No" ,并遍历结果 
33     if(myVec1[0] > 9){
34         cout << "No" << endl;
35         for(int i = 0; i < myVec1.size(); i++){
36             cout << myVec1[i];
37         }
38         cout << endl;
39         return 0;
40     }
41     //统计0-9数字各自的个数 
42     for(int i = 0; i < myVec.size(); i++){
43         myVec2[myVec[i]]++;
44         myVec3[myVec1[i]]++;
45     }
46 
47     int n;
48     for(n = 0; n < 10; n++){
49         if(myVec2[n] != myVec3[n]){    //如果对应数字个数不相同,则跳出循环 
50             break;
51         }
52     }
53 
54     if(n == 10){//如果n==10则说明没有异常输出 
55         cout << "Yes" << endl;
56         for(int i = 0; i < myVec1.size(); i++){
57             cout << myVec1[i];
58         }
59         cout << endl;
60     }else{
61         cout << "No" << endl;
62         for(int i = 0; i < myVec1.size(); i++){
63             cout << myVec1[i];
64         }
65         cout << endl;
66     }
67     return 0;
68 }

 

posted @ 2019-03-05 09:15  kakaluotedj  阅读(186)  评论(0)    收藏  举报