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.
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
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.
1234567899
Yes 2469135798
1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 #include<stack> 5 #include<algorithm> 6 #include<string> 7 using namespace std; 8 int Array[10]; 9 int main() 10 { 11 string num; 12 cin >> num; 13 int len = num.size(); 14 int flag = 0; 15 int temp = 0; 16 for (int i = len - 1; i >= 0; i--) 17 { 18 Array[num[i] - ‘0‘]++; 19 temp = (num[i] - ‘0‘) * 2 + flag; 20 flag = 0; 21 if (temp> 9) 22 { 23 temp %= 10; 24 flag = 1; 25 } 26 Array[temp]--; 27 num[i] = ‘0‘ + temp; 28 } 29 int flag1 = 0; 30 for(int i=0;i<10;i++) 31 if (Array[i]!=0) 32 { 33 flag1= 1; 34 break; 35 } 36 if (flag1) 37 { 38 cout << "No" << endl; 39 if (flag) 40 cout << "1" << num; 41 else 42 cout << num; 43 } 44 else 45 { 46 cout << "Yes" << endl; 47 if (flag) 48 cout << "1" << num; 49 else 50 cout << num; 51 } 52 53 }
1023 Have Fun with Numbers (20 分)
原文:https://www.cnblogs.com/57one/p/11937469.html