CSU 1002

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1002

1002: A+B (III)

            Time Limit: 1 Sec       Memory Limit: 128 Mb       Submitted: 4321       Solved: 1534    

Description

Your task is to Calculate a + b.

Input

There are multiple test cases. Each test case contains only one line. Each line consists of a pair of integers a and b ( 1 <= ab <=1016) , separated by a space. Input is followed by a single line with a = 0, b = 0, which should not be processed.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20
10000000000000000 10000000000000000
0 0

Sample Output

6
30
20000000000000000

Hint

Source

中南大学第五届大学生程序设计竞赛-热身赛

这个题跟hdu 1002做法是一样的,原先做hdu的时候是帮朋友纠错,就顺着他的思路去做的,这次自己做,总是错,其中有个变量一直打成另一个了,恰巧,我测试的数据还都是对的,感觉碰哒鬼哒,一直以为是自己的逻辑错了,后来再多测了几次数据才发现,有个变量敲错了,我真是服了我自己,每次都犯这种低级错误,而且还找不到错在哪里,我要先去哭一会。。。。。

#include<stdio.h>
#include<string.h>
char a[1000000],b[1000000];
int c[1000000];
int shu(char a)
{
    return (a-'0');
}
int main()
{
    int i;
    int a1,b1,s;

    while(scanf("%s %s",&a,&b)!=EOF)
    {

        if(a[0]=='0'&&b[0]=='0')
        {
            break;
        }
        a1=strlen(a);//a字符串的长度
        b1=strlen(b);//b字符串的长度
        s=(a1>b1)?a1:b1;
        int q=s;
        memset(c,0,sizeof(c));
        for(s; a1>0&&b1>0; s--)
        {
            c[s]=c[s]+shu(a[--a1])+shu(b[--b1]);
            if(c[s]/10)
            {
                c[s-1]++;
                c[s]=c[s]%10;

            }
        }
        while(a1>0)
        {
            c[s]=c[s]+shu(a[--a1]);
            if(c[s]/10)
            {
                c[s-1]++;
                c[s]=c[s]%10;
            }
            s--;
        }
        while(b1>0)
        {
            c[s]=c[s]+shu(b[--b1]);
            if(c[s]/10)
            {
                c[s-1]++;
                c[s]=c[s]%10;
            }
            s--;
        }
        for(i=0;i<=q;i++)
        {
            if(i==0&&c[i]==0)
            {
                i++;
            }
            printf("%d",c[i]);
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值