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 <stdio.h>
# include <stdlib.h>
# define MaxSize 10
// 使用int类型数,会超出精度范围
typedef long long int l_int;
void CountNumbers(int a[] ,int len,l_int num);
int main() {
int array[MaxSize] = {0},i;
l_int n;
int *p = array;
scanf("%lld",&n);
// 输入的数字进行翻倍操作后,要求原先的数字的个数依旧一致,只是构成的排列不相同
// 调用函数进行查看
CountNumbers(p,MaxSize,n);
// 现在开始翻倍操作
int array2[MaxSize] = {0};
int *q = array2;
CountNumbers(q,MaxSize,2*n);
// 现在开始比对
int flag = 0;
// 两个数一样的话直接跳出
if(n == 2*n) {
flag = 1;
} else {
for(i = 0;i < MaxSize;i++) {
if(array[i] != array2[i]) {
flag = 1;
break;
}
}
}
if(flag) printf("No\n");
else printf("Yes\n%lld",2*n);
return 0;
}
// 使用数组接收,加引用实现共享
void CountNumbers(int *p,int len,l_int num) {
// 因为是统计数中各个数字的数,但是数组无法直接传,因此使用指针
int value;
// 当p至少是二位数时进循环
while(num / 10) {
// 得到最后一位数
value = num % 10;
*(p + value) += 1;
num /= 10;
}
// 还剩下个位没处理
*(p + num) += 1;
}
提交截图:

为啥会报错,问题在于没有考虑到进位的情况,举个例子:比如我输入的数字是9位的,但是它一乘就成为10位了,因此这种情况下会导致数字的位数之间不一致造成误差!因此需要两个数的位数是否相同还是要判断的!懒得改了
于是直接女票了别人的:
看到的他们做的是直接使用字符的ASCII码相关操作,中心思想还是一致的,通过这个案例对指针的使用愈加熟悉了
#include <stdio.h>
#include <math.h>
#define SIZE 20
int main()
{
int num[SIZE],double_num[SIZE],bit=0; //bit记录原始数位数
int digit[10]={0,0,0,0,0,0,0,0,0,0}; //digit[i]代表输入数n中数字i出现的次数
int flag=1; //flag为1时,输出yes,为0时,输出no
char temp;
temp = getchar();
while (temp >= '0'&&temp <= '9') { //输入原始数
num[bit] = temp - '0';
digit[num[bit]]++; //记录原始数每个数字出现的次数
bit++;
temp = getchar();
}
for(int i=bit-1;i>=0;i--){ //原始数每位乘以2得到新数组
double_num[i]=num[i]*2;
}
//处理进位,double_num[0]不做进位处理,若>10,则判断flag为0
for(int i=bit-1;i>0;i--){
if(double_num[i]/10!=0){
double_num[i]=double_num[i]%10;
double_num[i-1]+=1;
}
digit[double_num[i]]--;
}
//判断原始数double后,是否位数与之前相同,若不同则flag为0
if(double_num[0]/10!=0){
flag=0;
}else{
digit[double_num[0]]--;
for(int j=0;j<10;j++){
if(digit[j]!=0){
flag=0;
break;
}
}
}
//输出结果
if(flag==0){
printf("No\n");
}else{
printf("Yes\n");
}
for(int i=0;i<bit;i++){
printf("%d",double_num[i]);
}
return 0;
}
博客内容涉及一个编程挑战,要求检查一个数翻倍后,其数字组成的排列是否仍由原数的数字组成。原代码在处理进位时出现问题,导致错误结果。解决方案是考虑数字翻倍后的进位情况,确保位数一致。提供的新代码通过字符ASCII码处理数字,并检查位数和数字出现次数来判断条件是否满足。

252

被折叠的 条评论
为什么被折叠?



