HDUJ 1002 A+B问题 高精度问题

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
此大数相加问题为简单行的。用java去做的话,有现成的数据模版用,步骤少。用C++做的话,只需要记住逆序相加就好。(每一次多要先把数组清零)


方法一:
<pre name="code" class="java">import java.math.BigInteger;
import java.util.Scanner;


public class ert {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		BigInteger a,b,c;
		int t = cin.nextInt();
		for(int i=1;i<=t;i++)
		{
			a = cin.nextBigInteger();
			b = cin.nextBigInteger();
			c = a.add(b);
			System.out.println("Case " + i + ":");
			System.out.println(a + " + " + b + " = " + c);
			if(i<t)    System.out.println();
		} 
	}
}



方法二:
#include<stdio.h>
#include<string.h>

int main()
{
    int T;
    scanf("%d",&T);
    for(int k=1;k<=T;k++)
    {
        char s1[1005],s2[1005];
        scanf("%s %s",s1,s2);
        int a[1005]={0},b[1005]={0},c[1005]={0},i;
        int l1=strlen(s1),l2=strlen(s2);
        for(i=0;i<l1;i++)
            a[i]=s1[l1-1-i]-48;
        for(i=0;i<l2;i++)
            b[i]=s2[l2-i-1]-48;
        int l=l1>l2?l1:l2;
        int j=0,s;
        for(i=0;i<l;i++)
        {
            s=a[i]+b[i]+j;
            c[i]=s%10;
            j=s/10;
        }
        if(j)   c[l++]=j;
         printf("Case %d:\n",k);
        printf("%s + %s = ",s1,s2);
        for(i=l-1;i>=0;i--)
            printf("%d",c[i]);
		printf("\n");
        if(k<T)  printf("\n");
    }
    
    return 0;
}



方法三:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int main()
{
    int T,w;
    scanf("%d",&T);
    for(w=1;w<=T;w++)
    {
        char a[1005],b[1005],c[1005]={0};
        int i,j,k,r,l1,l2,s;
        scanf("%s%s",a,b);
        l1=strlen(a);
        l2=strlen(b);
        strrev(a);
        strrev(b);
        i=j=r=k=s=0;
        while(i<l1&&j<l2)
        {
            s=(a[i]-48)+(b[j]-48)+r;
            r=s/10;
            c[k++]=s%10+48;
            i++;
            j++;
        }
        while(i<l1)
        {
            s=(a[i]-48)+r;
            r=s/10;
            c[k++]=s%10+48;
            i++;
        }
        while(j<l2)
        {
            s=(b[j]-48)+r;
            r=s/10;
            c[k++]=s%10+48;
            j++;
        }
        if(r)   c[k++]=r+48;
        strrev(a);
        strrev(b);
        strrev(c);
        printf("Case %d:\n",w);
        printf("%s + %s = %s\n",a,b,c);
        if(w!=T)  printf("\n");
    }

    return 0;
}

如有不懂,再看一遍。

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值