大数相加

题目为HDU 1002
链接:http://acm.hdu.edu.cn/showpro...
题目意思即是有两个超级大的数相加,求和
具体方法就是模拟两个数相加求和就好
AC code

请输入代码#include <stdio.h>
#include <string.h>
char a[1010];//数字a
char b[1010];//数字b
int suma[1010];   //存储数字a每一位的数组
int sumb[1010];   //存储数字b每一位的数组
int main()
{
    int T;
    scanf("%d",&T);
    int Case=0;
    while(T--)
    {
        scanf("%s%s",a,b);
        memset(suma,0,sizeof(suma));**//先把suma 和sumb 两个初始化为0;**
        memset(sumb,0,sizeof(sumb));
        int i,len=0;
        int lena=strlen(a);
        int lenb=strlen(b);
        //**进行数组存储**
        for(i=lena-1; i>=0; i--)
            suma[len++]=a[i]-'0';
        len=0;
        for(i=lenb-1; i>=0; i--)
            sumb[len++]=b[i]-'0';
        //下面就是模仿相加求和
        /*例如
            99999999                  00999
           +11111111                + 11001
           —————————                 ————————
           100000000                  12000
        就是大于10下一位进1;
                suma[i]-=10;
                suma[i+1]++;
        */
        for(i=0; i<1002; i++)
        {
            suma[i]=suma[i]+sumb[i];
            if(suma[i]>=10)
            {
                suma[i]-=10;
                suma[i+1]++;
            }
        }
        Case++;
        if(Case>1) printf("\n");
        printf("Case %d:\n",Case);
        printf("%s + %s = ",a,b);
        i=1001;
        while(suma[i]==0)
                i--;
        for(;i>=0;i--)
            printf("%d",suma[i]);
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值