您的位置:首页 > 编程语言 > C语言/C++

1023. Have Fun with Numbers (20)

2016-01-23 20:09 435 查看
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


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

string doubleNum(string s){
string result;
int overflow = 0;

for(auto rit = s.rbegin(); rit != s.rend(); ++rit){
int num = (*rit - '0') * 2 + overflow;

result = char(num % 10 + '0') + result;
overflow = num / 10;
}

if(overflow) result = char(overflow + '0') + result;

return result;
}

int main(){
string s;
cin >> s;

string ss = doubleNum(s);
string out(ss);

sort(begin(s), end(s));
sort(begin(ss), end(ss));

cout << (s == ss ? "Yes" : "No") << endl;
cout << out;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息