1023 Have Fun with Numbers
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
大整数乘法
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char num[25];
int num_int[25];
int count_num[10];
int result[25] = {0};
int main()
{
cin >> num;
memset(count_num, 0, sizeof(count_num));
memset(num_int, 0, sizeof(num_int));
int len = strlen(num);
for (int i = 0; i < len; i++) {
num_int[len - i - 1] = num[i] - '0';
}
for (int i = 0; i < len; i++) {
count_num[num_int[i]]++;
}
int carry = 0;
int len_num = 0;
for (int i = 0; i < len; i++) {
int temp = num_int[i] * 2 + carry;
result[len_num++] = temp % 10;
carry = temp / 10;
}
while (carry != 0) { // 如果最后还进位
result[len_num++] = carry % 10;
carry = carry / 10;
}
for (int i = 0; i < len_num; i++) {
count_num[result[i]]--;
}
int flag = 0;
for (int i = 0; i < 10; i++) {
if (count_num[i] != 0) {
cout << "No" << endl;
flag = 1;
break;
}
}
if (flag == 0) {
cout << "Yes" << endl;
}
for (int i = len_num - 1; i >= 0; i--) {
cout << result[i];
}
return 0;
}