完整C代码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
时间: 2023-12-29 14:03:54 浏览: 191
以下是完整的C代码实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char num[21];
scanf("%s", num);
int len = strlen(num);
char double_num[22];
int i, j;
for (i = len - 1, j = 0; i >= 0; i--, j++) {
int digit = num[i] - '0';
int double_digit = digit * 2;
if (double_digit >= 10) {
double_num[j] = double_digit % 10 + '0';
j++;
double_num[j] = '1';
} else {
double_num[j] = double_digit + '0';
}
}
double_num[j] = '\0';
if (strlen(double_num) != len) {
printf("No\n%s\n", double_num);
return 0;
}
int num_count[10] = {0};
int double_count[10] = {0};
for (i = 0; i < len; i++) {
num_count[num[i] - '0']++;
double_count[double_num[i] - '0']++;
}
for (i = 0; i < 10; i++) {
if (num_count[i] != double_count[i]) {
printf("No\n%s\n", double_num);
return 0;
}
}
printf("Yes\n%s\n", double_num);
return 0;
}
```
这里我们同样先读入输入的字符串,然后按位从低到高计算出其对应的双倍数。由于双倍数可能会进位,因此我们需要在输出的字符串中分别填入高位和低位的数字。最后,我们比较原始数和双倍数中每个数字出现的次数,如果不一致,则说明双倍数不是原始数的排列。
需要注意的是,我们在输出字符串时需要判断其长度是否和原始数相等,如果不相等,则说明双倍数的位数比原始数多,此时直接输出即可。
阅读全文