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:2469135798
#include <iostream> #include <string> using namespace std; int add(int n) { return 1<<n; } void compare(string str) { int len = str.length(); int carry = 0; int tmp; int sign1 = 0; int sign2 = 0; while(len) { sign1 += add(str[len-1] - '0'); tmp = (str[len-1] - '0') * 2; str[len-1] = '0' + tmp%10 + carry; sign2 += add(str[len-1] - '0'); carry = (tmp + carry)/10; len--; } if(sign1 == sign2) { cout<<"Yes"<<endl; } else { cout<<"No"<<endl; } if(carry == 0) { cout<<str<<endl; } else { cout<<carry<<str<<endl; } } int main() { string in; cin>>in; compare(in); return 0; }
1023. Have Fun with Numbers (20)
原文地址:http://blog.csdn.net/jason_wang1989/article/details/44021107