读书人

杭电oj的1002(大数加法)哪错了?该

发布时间: 2012-04-06 12:22:24 作者: rapoo

杭电oj的1002(大数加法),哪错了?
题目:

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

我的程序:

C/C++ code
#include <string.h>#include <stdio.h>#define N 1000int main(void){    char a[N], b[N];    int c[N+1]={0};    int t;    scanf("%d", &t);    int loops;    for(loops = 1; loops<=t; loops++) {        scanf("%s %s", a, b);        int la = strlen(a);        int lb = strlen(b);        int length = (la > lb ? la : lb)+1;        int i, j, k=0;        for(i=la-1, j=lb-1; i>=0&&j>=0; i--, j--) {            c[k++] = a[i]-'0'+b[j]-'0';        }        while(i>=0)            c[k++] = a[i--]-'0';        while(j>=0)            c[k++] = b[j--]-'0';        int carrier = 0;        for(i=0; i<k; i++) {            c[i] += carrier;            if(c[i] > 9) {                c[i] %= 10;                carrier = 1;            }        }        printf("Case %d:\n", loops);        printf("%s + %s = ", a, b);        if(carrier == 1)            printf("1");        for(i=k-1; i>=0; i--)            printf("%d", c[i]);        printf("%s", loops==t ? "\n" : "\n\n");    }    return 0;}


[解决办法]
C/C++ code
include <iostream>using namespace std;char a[1000],b[1000];int c[1001],d[1001];int max(int i,int j){    return (i>j?i:j);}int main(){    int t,i,j,l,m=1;    cin>>t;    while(t--)    {    for(i=0;i<1000;i++)    {        a[i]=b[i]='\0';        c[i]=d[i]=0;    }    c[1000]=d[1000]=0;    if(m!=1)    cout<<endl;    cin>>a;    cin>>b;    for(i=0;a[i]!='\0';i++);        c[0]=i;    for(i=0;b[i]!='\0';i++);        d[0]=i;    cout<<"Case "<<m<<":"<<endl;    for(i=0,j=c[0];j>0;j--,i++)    {        c[j]=a[i]-'0';        cout<<c[j];    }    cout<<" + ";    for(i=0,j=d[0];j>0;j--,i++)    {        d[j]=b[i]-'0';        cout<<d[j];    }    cout<<" = ";    l=max(c[0],d[0]);    for(i=1;i<l+2;i++)    {        c[i]=c[i]+d[i];        if(c[i]>=10)        {            c[i]%=10;            c[i+1]++;        }    }    if(c[l+1]!=0)    l++;    c[0]=l;    for(i=l;i>0;i--)    {        cout<<c[i];    }    cout<<endl;    m++;    }    return 0;}
[解决办法]
while(i>=0)
c[k++] = a[i--]-'0';
while(j>=0)
c[k++] = b[j--]-'0';
应该是大于0吧,等于0是,再做减法会成负数。
[解决办法]
测试和提交有什么区别呢?
[解决办法]
我记得杭电ACM论坛上有人用__int64做的~没看程序,用字符串思路是对的,不过中间肯定有地方出问题啦
[解决办法]
字符串思路
[解决办法]
for(i=0; i<k; i++) {
c[i] += carrier;
if(c[i] > 9) {
c[i] %= 10;
carrier = 1;
}
else{
carrier = 0;
}
}
好像你赋值carrier之后就一直是1了,

读书人网 >软件架构设计

热点推荐