hdoj 巨大数相加 (不知道哪里错了,被wa,求大牛指导)

weixin_41109282 2017-11-23 07:20:27
Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.





Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.





Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.





Sample Input

2
1 2
112233445566778899 998877665544332211





Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110








#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
char a[1010];
char b[1010];
int num1[1000];
int num2[1000];
int sum[1001];
int num1_len;
int num2_len;
int t;
int t1;
int i;
int x;
int y;

scanf("%d",&t);
for(t1=1;t1<=t;t1++)
{
memset(num1,0,sizeof(int)*1001);
memset(num2,0,sizeof(int)*1001);
memset(sum,0,sizeof(int)*1001);
for(i=0;i<1010;i++)
{
a[i]='\0';
b[i]='\0';


} //清0
x=0;
scanf("%s %s",a,b);

num1_len=strlen(a);
num2_len=strlen(b);
for(i=num1_len-1;i>=0;i--)
{
num1[i]=(int)a[x]-(int)'0';
x++;
}
x=0;
for(i=num2_len-1;i>=0;i--) //反序存储
{
num2[i]=(int)b[x]-(int)'0';
x++;
}

if(num1_len>=num2_len)
{

for(i=0;i<num1_len;i++)
{
if(num1[i]+num2[i]>9)
{
sum[i]+=num1[i]+num2[i]-10;
sum[i+1]++;
}
else
{
sum[i]+=num1[i]+num2[i];
}
}
}
else
{
for(i=0;i<num2_len;i++)
{
if(num1[i]+num2[i]>9)
{
sum[i]+=num1[i]+num2[i]-10;
sum[i+1]++;
}
else
{
sum[i]+=num1[i]+num2[i];
}
}
}

printf("Case %d:\n",t1);
printf("%s + %s = ",a,b);
for(i=1000;i>=0;i--)
{
if(sum[1000]==1)
{
y=1000;
break;
}


if(sum[i]==0 && sum[i-1]!=0)
{
y=i-1;
break;
}
}
for(i=y;i>=0;i--)
{
printf("%d",sum[i]);
}
printf("\n");
if(t1!=t)
printf("\n");
}


return 0;
}
...全文
64 1 点赞 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
摆渡过江 2017-11-23
#include <stdio.h> #include <stdlib.h> #include <string.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { char a[1010]; char b[1010]; int num1[1000]; int num2[1000]; int sum[1001]; int num1_len; int num2_len; int t; int t1; int i; int x; int y; scanf("%d",&t); for(t1=1;t1<=t;t1++) { memset(num1,0,sizeof(int)*1001); memset(num2,0,sizeof(int)*1001); memset(sum,0,sizeof(int)*1001); for(i=0;i<1010;i++) { a[i]='\0'; b[i]='\0'; } //清0 x=0; scanf("%s %s",a,b); num1_len=strlen(a); num2_len=strlen(b); for(i=num1_len-1;i>=0;i--) { num1[i]=(int)a[x]-(int)'0'; x++; } x=0; for(i=num2_len-1;i>=0;i--) //反序存储 { num2[i]=(int)b[x]-(int)'0'; x++; } if(num1_len>=num2_len) { for(i=0;i<num1_len;i++) { if(num1[i]+num2[i]+sum[i]>9) { sum[i]+=num1[i]+num2[i]-10; sum[i+1]++; } else { sum[i]+=num1[i]+num2[i]; } } } else { for(i=0;i<num2_len;i++) { if(num1[i]+num2[i]+sum[i]>9) { sum[i]+=num1[i]+num2[i]-10; sum[i+1]++; } else { sum[i]+=num1[i]+num2[i]; } } } printf("Case %d:\n",t1); printf("%s + %s = ",a,b); for(i=1000;i>=1;i--) { if(sum[1000]==1) { y=1000; break; } if(sum[i]==0 && sum[i-1]!=0) { y=i-1; break; } if(i-1==0 && sum[i-1]==0 ) { printf("%d",0); y=-1; } } for(i=y;i>=0;i--) { printf("%d",sum[i]); } printf("\n"); if(t1!=t) printf("\n"); } return 0; }
  • 打赏
  • 举报
回复
相关推荐
发帖
C语言
加入

6.5w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2017-11-23 07:20
社区公告
暂无公告