pat 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 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 <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <map> 6 #include <stack> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #define LL long long 11 using namespace std; 12 const int MAX = 30; 13 14 long long A[15] = {0}, B[15] = {0}, len; 15 char s[MAX], s2[MAX]; 16 bool is_equal() 17 { 18 for (int i = 0; i <= 9; ++ i) 19 if (A[i] != B[i]) return false; 20 return true; 21 } 22 23 void calcS2() 24 { 25 int b = 0, temp[30]; 26 for (int i = 0, j = len - 1; i < len; ++ i, -- j) 27 { 28 if (j != -1) b += 2 * (s[j] - '0'); 29 temp[i] = b % 10; 30 b /= 10; 31 if (b > 0 && i == len - 1) ++ len; 32 } 33 for (int i = 0, j = len - 1; i < len; ++ i, -- j) 34 s2[j] = char('0' + temp[i]); 35 } 36 37 int main() 38 { 39 // freopen("Date1.txt", "r", stdin); 40 scanf("%s", &s); 41 len = strlen(s); 42 for (int i = 0; i < len; ++ i) 43 A[s[i] - '0'] ++; 44 calcS2(); 45 len = strlen(s2); 46 for (int i = 0; i < len; ++ i) 47 B[s2[i] - '0'] ++; 48 if (is_equal()) cout <<"Yes" <<endl <<s2 <<endl; 49 else cout <<"No" <<endl <<s2 <<endl; 50 return 0; 51 }