新人大数加法求解

Kirinohana 2014-12-20 12:52:34
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 <string.h>
int main()
{
char a[1001],b[1001]; //a b 两个数
int c,i,j,k,s[1001]; //c 进位 i,j,k 下标 s[1001] 相加后的数
int n,count=1; //n 需输入组数 count 当前组数
scanf("%d%*c",&n);
while(n-->0)
{
scanf("%s%s",&a,&b);
k=0;
c=0;
for(i=strlen(a)-1,j=strlen(b)-1;i>=0&&j>=0;i--,j--)
{
s[k++]=(a[i]-'0'+b[j]-'0'+c)%10;
c=(a[i]-'0'+b[j]-'0'+c)/10;
}
if(i>=0) //如果a数长
for(;i>=0;i--)
{
s[k++]=(a[i]-'0'+c)%10;
c=(a[i]-'0'+c)/10;
}
else //如果b数长
for(;j>=0;j--)
{
s[k++]=(b[j]-'0'+c)%10;
c=(b[j]-'0'+c)/10;
}
if(c) //最后进位
s[k++]=c;
printf("\n#Case %d:\n%s + %s = ",count++,a,b);
for(k=k-1;k>=0;k--)
printf("%d",s[k]);
printf("\n");
}
return 0;
}


请问各位前辈哪里错了,实在找不出,先谢谢各位前辈了~~~~~~~~
...全文
147 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-12-22
  • 打赏
  • 举报
回复
仅供参考
#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
Kirinohana 2014-12-22
  • 打赏
  • 举报
回复
引用 4 楼 a138762698828 的回复:
scanf("%d%*c",&n); while(n-->0) { scanf("%s%s",&a,&b); 两个scanf 就有问题了 第一个scanf 有两个输出 只有一个参数 n, 第二个scanf , a b都是数组, 输入就不需要取地址了 他们本来就指向数组的首地址!
感谢!其实第一个%*c没问题,只是用来过滤换行符,问题出在第二个scanf,去掉&后终于Presentation Error了,再结合3楼答案就AC了!
Kirinohana 2014-12-22
  • 打赏
  • 举报
回复
引用 3 楼 niechangxu 的回复:
你的 # 位置错了吧 应该是 Case #1: ,还有就是这句话Output a blank line between two test cases. 两组测试数据之间有空行,你这个空行太多了吧。。
感谢!那个#是多余的,还有空行问题,格式感觉真心难玩,老是Presentation Error!
a138762698828 2014-12-22
  • 打赏
  • 举报
回复
scanf("%d%*c",&n); while(n-->0) { scanf("%s%s",&a,&b); 两个scanf 就有问题了 第一个scanf 有两个输出 只有一个参数 n, 第二个scanf , a b都是数组, 输入就不需要取地址了 他们本来就指向数组的首地址!
曾经最动心 2014-12-21
  • 打赏
  • 举报
回复
你的 # 位置错了吧 应该是 Case #1: ,还有就是这句话Output a blank line between two test cases. 两组测试数据之间有空行,你这个空行太多了吧。。
Kirinohana 2014-12-20
  • 打赏
  • 举报
回复
引用 1 楼 fly_dragon_fly 的回复:
先说一下那里错了
OJ那里错误信息是Wrong Answer
fly_dragon_fly 2014-12-20
  • 打赏
  • 举报
回复
先说一下那里错了

69,371

社区成员

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

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