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 19 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 10:03:59 浏览: 75
首先,我们需要将输入的数转换成字符串,并计算出它的长度。接着,我们将该数乘以2,再将结果转换成字符串,并且将它的长度与原始字符串的长度进行比较。如果长度不同,那么乘以2得到的数字肯定不是原始数字的排列。如果长度相同,那么我们需要逐一检查乘以2得到的数字中的每一个数字,确保它们在原始数字中都存在且出现的次数相同。如果满足这个条件,那么乘以2得到的数字就是原始数字的排列,否则就不是。
下面是实现这个算法的代码:
相关问题
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.
注意,数字123456789是一个由数字1到9组成的9位数,没有重复。将它翻倍,我们将得到246913578,这恰好是另一个由数字1到9组成的9位数,只是排列不同。现在你应该检查如果我们再次将其翻倍的结果是什么!现在你应该检查是否还有更多具有此属性的数字。也就是说,将一个有k位数字翻倍,你需要判断结果数字是否只由原始数字的排列组成。
完整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
以下是完整的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;
}
```
这里我们同样先读入输入的字符串,然后按位从低到高计算出其对应的双倍数。由于双倍数可能会进位,因此我们需要在输出的字符串中分别填入高位和低位的数字。最后,我们比较原始数和双倍数中每个数字出现的次数,如果不一致,则说明双倍数不是原始数的排列。
需要注意的是,我们在输出字符串时需要判断其长度是否和原始数相等,如果不相等,则说明双倍数的位数比原始数多,此时直接输出即可。
阅读全文