A + B Problem II(hdu1002,大数加法)

这是一道基本的大数题,求两个大数的加法


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代码:

#include<stdio.h>
#include<string.h>
int jx(char *a,char *b,char *c)
{
    int i,t,la,lb;


    //分别测量字符数组a,b的长度


    la=strlen(a)-1;
    lb=strlen(b)-1;


    //计算他们的和并赋给c数组,c数组是倒过来存的,即c[0]=a的个位+b的个位....


    for(t=0,i=0;la>=0||lb>=0;i++,la--,lb--)   //直至a,b均没有该位时跳出(如 千位)
    {
        //从a和b的低位往高位计算
        //其中的t表示来自上一位的进位
        //la与lb大小一般不同,但他们表示a和b中的同一位数(即都是个位,十位...)


        if(la<0)                              //b有该位,a已经没有了
        {
            c[i]=b[lb]-'0'+t;                 //b[lb]必须变为数字后再相加(即减去'0')
            t=c[i]/10;                        //计算进位
            c[i]=c[i]%10;                     //去除进位
        }
        else if(lb<0)                         //a有该位,b已经没有了
        { 
            c[i]=a[la]-'0'+t;
            t=c[i]/10;
            c[i]=c[i]%10;
        }
        else if(la>=0&&lb>=0)                 //a,b均有该位
        {
            c[i]=a[la]+b[lb]-2*'0'+t;         //a[la]和b[lb]都是字符,必须变为数字后再相加(即分别减去'0')
            t=c[i]/10;
            c[i]=c[i]%10;
        }
    }   
    if(t)                                     //适用于“3+8”这种情况,都没有十位,但进位还有
    { 
        c[i++]=t;
    }
    return i;                                 //返回c数组的长度
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n,i,t;
    char a[1005],b[1005],c[1005];
    scanf("%d",&n);
    getchar();                                //吃回车
    for(i=1;i<=n;i++)       
    {
        scanf("%s%s",a,b);                    //字符数组a,b分别读入两大数
        if(i!=1)                              //两组数据间空行
        {
            printf("\n");
        }
        t=jx(a,b,c);                          //c数组存两大数相加的结果
        printf("Case %d:\n",i);
        printf("%s + %s = ",a,b);
        for(t--;t>=0;t--)                     //c数组是倒过来存的,但要从高位开始输出(故从t到0)
        {
            printf("%c",c[t]+'0');            //c里的元素实际是int,要以%c输出,必须加'0'变为字符
        }
        printf("\n");
    }
    return 0;
}



java中有个大数类,用起来比较方便


java代码:

import java.util.Scanner;                     //引入java中的输入类所在包
import java.math.BigInteger;                  //引入java中的大数类所在包
public class Main{
    public static void main(String args[])
    {
        Scanner read=new Scanner(System.in);  //创建一个输入类对象read
        int i=1,t=read.nextInt();             //读入数据组数
        BigInteger a,b,c;                     //创建a,b,c三个大数对象
        while(i<=t)
        {
            a=read.nextBigInteger();          //读入大数a
            b=read.nextBigInteger();          //读入大数b
            c=a.add(b);                       //大数a+大数b
            System.out.println("Case "+i+":");
            System.out.println(a+" + "+b+" = "+c);
            if(i!=t)                          //两组数据间空行
            {
                System.out.println("");
            }
            i++;
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值