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
Ω
趣味数,是指乘2后的数是原数中所有数字的一个排列的数。给出一个不超过20位的数,我们需要判断该数是否为趣味数,同时输出乘2后的数。
由于最大是20位数,因此不能直接用整型参与运算,需要自己实现大数乘2算法,然后用一个数字计数vector
统计原数中0-9各数字个数,在进行乘2运算每产生一位都在计数器中减去相应数字的个数,最后计数器中若都为0则说明为趣味数,反之则不是。
C☺DE
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string num, num2 = "";
cin >> num;
vector<int> check(10, 0);
for (auto &c: num)
++check[c - '0'];
int flag = 0, single;
for (auto i = num.rbegin(); i != num.rend(); ++i)
{
single = 2 * (*i - '0') + flag;
flag = single / 10;
num2 = to_string(single % 10) + num2;
--check[single % 10];
}
if (flag)
{
num2 = to_string(flag) + num2;
--check[flag];
}
bool isFun = true;
for (auto k: check)
if (k != 0)
{
isFun = false;
break;
}
cout << (isFun ? "Yes" : "No") << endl << num2 << endl;
}