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
大数运算加HashTable
#include<cstdio>
#include<cstring>
struct bign {
int d[25];
int len;
bign() {
memset(d, 0, sizeof(d));
len = 0;
}
};
int Hashtable1[10] = { 0 }, Hashtable2[10] = { 0 };
bign change(char s[]) {
bign a;
a.len = strlen(s);
for (int i = 0; i < a.len; i++) {
a.d[i] = s[a.len - 1 - i] - '0';
Hashtable1[a.d[i]]++;
}
return a;
}
bign Double(bign a) {
bign b;
b.len = a.len;//先令长度相等,不等也就差一位
int carry = 0, temp;
for (int i = 0; i < a.len; i++) {
temp = a.d[i] * 2 + carry;
b.d[i] = temp % 10;
carry = temp / 10;
Hashtable2[b.d[i]]++;
}
if (carry) {
b.len++;
b.d[b.len - 1] = carry;
}
return b;
}
int main() {
char s[25];
scanf("%s", s);
bign a, b;
a = change(s);
b = Double(a);
int i;
for ( i = 0; i < 10; i++) {
if (Hashtable1[i] != Hashtable2[i]) {
printf("No\n");
break;
}
}
if (i == 10) printf("Yes\n");
for (i = b.len - 1; i >= 0; i--) printf("%d", b.d[i]);
return 0;
}