1023 Have Fun with Numbers
Statement
Metadata
- 作者: CHEN, Yue
- 单位: 浙江大学
- 代码长度限制: 16 KB
- 时间限制: 400 ms
- 内存限制: 64 MB
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
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
Sample Output
Solution
#include <bits/stdc++.h>
using namespace std;
string time(string s) {
int i, len = s.size(), num = 0;
string s1;
for (i = len - 1; i >= 0; i--) {
num += (s[i] - '0') * 2;
s1 += num % 10 + '0';
num /= 10;
}
if (num)
s1 += num + '0';
return s1;
}
int main() {
string s, s1;
cin >> s;
s1 = time(s);
int len1 = s1.size(), i, ans[10] = {0}, len = s.size();
for (i = 0; i < len; i++) ans[s[i] - '0']++;
for (i = 0; i < len; i++) ans[s1[i] - '0']--;
for (i = 0; i < 10; i++) {
if (ans[i]) {
cout << "No\n";
goto a;
}
}
cout << "Yes\n";
a:
for (i = len1 - 1; i >= 0; i--) cout << s1[i];
cout << endl;
}