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 file 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
来源: <http://www.patest.cn/contests/pat-a-practise/1023>
#pragma warning(disable:4996)
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string s1 = "12345678901234567890",s2="12345678901234567890";
cin >> s1;
int n = s1.length();
int jinwei = 0;
for (int i = 0; i < n; i++) {
if(s1[i]<‘5‘)
s2[i] = 2 * (s1[i] - ‘0‘)+‘0‘;
else {
if (i > 0) {
s2[i] = 2 * (s1[i] - ‘0‘) - 10+‘0‘;
s2[i - 1] += 1;
}
if (i == 0) {
s2[i] = 2 * (s1[i] - ‘0‘) - 10+‘0‘;
jinwei = 1;
}
}
}
if (jinwei == 1) {
cout << "No" << endl;
cout << 1;
for (int i = 0; i < n; i++) {
cout << s2[i];
}
return 0;
}
int a[20] = { 0 };
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s2[i] == s1[j] && a[j] == 0)
a[j]++;
}
}
int count = 0;
for (int i = 0; i < n; i++)
if (a[i] == 1)
count++;
if (count == n) {
cout << "Yes"<<endl;
for (int i = 0; i < n; i++) {
cout << s2[i];
}
}
else {
cout << "No"<<endl;
for (int i = 0; i < n; i++) {
cout << s2[i];
}
}
return 0;
}