HDU-1002 A + B Problem II Java大数

  一道最基本的大数相加题,java果然强大啊。

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

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

  以下是以前写的C++代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX 1000

char a[MAX+1],b[MAX+1];
int T;

int main()
{
    scanf("%d",&T);
    for(int w=1;w<=T;++w)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%s%s",a,b);
        printf("Case %d:\n",w);
        printf("%s + %s = ",a,b);
        int len1=strlen(a);
        int len2=strlen(b);
        int fpla=MAX-( len1>len2 ? len1:len2);
        for(int i=len1-1,k=MAX;i>=0;--i,--k) 
            a[k]=(a[i]-'0'),a[i]=0;
            
        for(int i=len2-1,k=MAX;i>=0;--i,--k) 
            b[k]=(b[i]-'0'),b[i]=0;
            
        for(int i=MAX;i>=fpla;--i)
            a[i]+=b[i]; 
        for(int i=MAX;i>=fpla;--i)
        {
            int c=a[i]/10;
            a[i-1]+=c;
            a[i]%=10;
        } 
        for(int i=fpla+1;i<=MAX;++i)
            printf("%d",a[i]);
        puts("");
        if(w<T)
            puts("");
    }
    return 0;
}

  对比之下,不解释了。

posted @ 2011-08-14 09:53  沐阳  阅读(2441)  评论(0编辑  收藏  举报