加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数相加

发布时间:2020-12-14 04:52:32 所属栏目:大数据 来源:网络整理
导读:题目为HDU 1002 链接:http://acm.hdu.edu.cn/showpro... 题目意思即是有两个超级大的数相加,求和 具体方法就是模拟 两个数相加求和 就好 AC code 请输入代码#include stdio.h#include string.hchar a[1010];//数字achar b[1010];//数字bint suma[1010]; //

题目为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,sizeof(suma));**//先把suma 和sumb 两个初始化为0;**
        memset(sumb,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
           +22222111                + 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 = ",b);
        i=1001;
        while(suma[i]==0)
                i--;
        for(;i>=0;i--)
            printf("%d",suma[i]);
        printf("n");
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读