(附翻译)Have Fun with Numbers (20)的问题

Last_stardust 2015-07-22 03:58:01
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

【题目大意】:有一类像123456789的数,将其乘2后的数位组成是一样的(246913578)。同样的数还有1234567899等等
【疑问】这里我想问的是,先不考虑任何数据结构的应用,只是存粹的C程序设计的话,下面的代码为何不能通过题目的检测点

#include<stdio.h>
#include<stdlib.h>
int *devideNumber(long long int );//这个函数负责将数各位分解,无特别

static int cnt=0;

int main(){
long long int x,b[20];//将所给数作为long long int型存储
int i,NotMatch=0,mark;
scanf("%d",&x);
int *a=devideNumber(x);//将分解后的数字放进malloc的空间内,无特别

while(cnt){
b[cnt-1]=*a++;//将分解后的数字倒着装入数组b中(因为分解是倒着分的)
cnt--;
}
x=2*x;
a=devideNumber(x);//将翻倍后的x再次分解,无特别
while(1){
for(i=0,mark=0;i<cnt;i++){
if(*a==b[i]||*a==0){
NotMatch++;//将两次分解结果匹配,匹配数NotMatch与位数cnt相同则YES
b[i]=-1;//每匹配成功一位就置空以应对相同的情况(如1234567899)
mark=1;//mark是输出控制,没什么特别的
a++;
break;
}
}
if(NotMatch==cnt&&mark==1){
printf("YES\n");
while(cnt--)
printf("%d",*(a---1));
break;
}else if(mark==0){
printf("NO");
break;
}
}
}
int *devideNumber(long long int x){
int i;
int* a=(int *)malloc(20*sizeof(int));
for(i=0;x>0;i++){
a[i]=x%10;
x=x/10;
cnt++;
}
return a;
}
...全文
661 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2015-09-01
  • 打赏
  • 举报
回复
应该是用数组存放吧,20位数ll也放不下
Last_stardust 2015-07-24
  • 打赏
  • 举报
回复
引用 12 楼 zhao4zhong1 的回复:
仅供参考:
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
    scanf("%d",&n);
    for (j=0;j<n;j++) {
        scanf("%s%s",a1,a2);

        L=strlen(a1);
        for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';

        L=strlen(a2);
        for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';

        for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];

        for (i=0;i<MAXLEN;i++) {
            if (v3[i]>=10) {
                v3[i+1]+=v3[i]/10;
                v3[i]=v3[i]%10;
            }
        }

        printf("Case %d:\n", j+1);
        printf("%s + %s = ", a1, a2);

        z=0;
        for (i=MAXLEN-1;i>=0;i--) {
            if (z==0) {
                if (v3[i]!=0) {
                    printf("%d",v3[i]);
                    z=1;
                }
            } else {
                printf("%d",v3[i]);
            }
        }
        if (z==0) printf("0");

        printf("\n");
    }
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110
非常感谢
Last_stardust 2015-07-24
  • 打赏
  • 举报
回复
引用 9 楼 sandshaw 的回复:
不知道是不是你画蛇添足了, 题目说了输入一个正整数, 不存在负数和0的情况.
哦。。原来如此
赵4老师 2015-07-23
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
    scanf("%d",&n);
    for (j=0;j<n;j++) {
        scanf("%s%s",a1,a2);

        L=strlen(a1);
        for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';

        L=strlen(a2);
        for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';

        for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];

        for (i=0;i<MAXLEN;i++) {
            if (v3[i]>=10) {
                v3[i+1]+=v3[i]/10;
                v3[i]=v3[i]%10;
            }
        }

        printf("Case %d:\n", j+1);
        printf("%s + %s = ", a1, a2);

        z=0;
        for (i=MAXLEN-1;i>=0;i--) {
            if (z==0) {
                if (v3[i]!=0) {
                    printf("%d",v3[i]);
                    z=1;
                }
            } else {
                printf("%d",v3[i]);
            }
        }
        if (z==0) printf("0");

        printf("\n");
    }
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110
赵4老师 2015-07-23
  • 打赏
  • 举报
回复
楼主的OutPut后面应加一句printf("\n");吧。 参考4楼。
sandshaw 2015-07-23
  • 打赏
  • 举报
回复
内存malloc出来, 用完记得free.
sandshaw 2015-07-23
  • 打赏
  • 举报
回复
不知道是不是你画蛇添足了, 题目说了输入一个正整数, 不存在负数和0的情况.
Last_stardust 2015-07-22
  • 打赏
  • 举报
回复
引用 5 楼 sandshaw 的回复:

scanf("%lld",&x);
改了%lld输入以后,还考虑了负数和0的问题,之后的结果只有部分通过,不知为何:

#include<stdio.h>
#include<stdlib.h>
int *DevideNumber(long long int ); 

void OutPut(int,int,int *,int);//输出结果函数,参数:Correct:Yes/NO
static int cnt=0;									//Plus_Minus:正/负					
													//int*:分解结果的地址
int main(){											//cnt:位数
	long long  int x,b[20];
	int i,NotMatch=0,Mark,Plus_Minus,Correct;
	scanf("%lld",&x);
	
	if(x>0){
		Plus_Minus=1;
	}else if(x==0){
		Corret=1;
		Plus_Minus=0;
	}else if(x<0){
		x=-x;
		Plus_Minus=-1;
	}
	
	int *a=DevideNumber(x);
	while(cnt){
		b[cnt-1]=*a++;
		cnt--;
	}
	x=2*x;
	int* save=a=DevideNumber(x);
	while(1){
		for(i=0,Mark=0;i<cnt;i++){
			if(*a==b[i]){	
				b[i]=-1; 
				Mark=1; 
				NotMatch++;
				a++;
				break;
			}
		}
		if(NotMatch==cnt&&Mark==1||x==0){
			Correct=1;
			printf("Yes\n");
			OutPut(Correct,Plus_Minus,a,cnt);
			break;
		}else if(Mark==0){
			Correct=0;
			printf("No\n");
			OutPut(Correct,Plus_Minus,save,cnt);
			break;
		}
	}
	
	return 0;
}
int *DevideNumber(long long int x){
	int i;
	int* a=(int *)malloc(20*sizeof(int));
	for(i=0;x!=0;i++){
		a[i]=x%10;
		x=x/10;
		cnt++;
	}
	return a;
}

void OutPut(int Correct,int Plus_Minus,int *p,int cnt){
	if(Plus_Minus==-1)
		printf("-");
	else if(Plus_Minus==0)
		printf("0");
	if(Correct==1){
		while(cnt--)
			printf("%d",*(p---1));
	}else if(Correct==0){
		while(cnt--)
			printf("%d",*(p+cnt));
	}		
}
Last_stardust 2015-07-22
  • 打赏
  • 举报
回复
引用 5 楼 sandshaw 的回复:

scanf("%lld",&x);
非常感谢!我一直在想为什么每次用%d输入都会溢出
赵4老师 2015-07-22
  • 打赏
  • 举报
回复
引用 5 楼 sandshaw 的回复:

scanf("%lld",&x);
printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方,而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。
sandshaw 2015-07-22
  • 打赏
  • 举报
回复

scanf("%lld",&x);
Last_stardust 2015-07-22
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
printf("NO"); 改为 printf("NO\n"); ?
不过您的回答倒是提醒了我,可能是输出方面没有仔细读要求。我忽视了Yes的大小写问题,还有No以后仍要求输出数字。非常感谢您的启发!
赵4老师 2015-07-22
  • 打赏
  • 举报
回复
关于自己是否适合编程的很简单的测试: 在报纸或杂志上随便找一段约1000字的文章,在Word中输入一遍。输完后再参考下面答案: A里面有10处以上文字或标点错误 B里面没有文字或标点错误并敢为此跟人打赌 C里面没有文字或标点错误并且字体和排版完全与原稿一致 D打印在半透明的纸上和原稿重叠在一起检查一模一样,且自我感觉很有成就感 A不适合编程(理由:打字准确度偏低、粗心大意) B初级程序员(理由:打字准确度很高、认真细致、自信、理解全角半角概念) C高级程序员(理由:在B的基础上理解字体和排版也是电脑打印的重要因素、但相比D还不够偏执、精益求精、结果可验证) D软件项目经理(理由:能针对项目给出令人信服的细致到极点的需求说明和典型测试用例。用户几乎挑不出毛病。专业!) 如果想从A变成B的话,到我的资源http://download.csdn.net/detail/zhao4zhong1/4084259里面下载“适合程序员的键盘练习”
Last_stardust 2015-07-22
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
printf("NO"); 改为 printf("NO\n"); ?
检测还是没通过,应该不是这样的小问题。所有的检测点都没过,一定是原理哪里错了
赵4老师 2015-07-22
  • 打赏
  • 举报
回复
printf("NO"); 改为 printf("NO\n"); ?

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧