Java大数相加的问题(杭电ACM1047)

原题:

Integer Inquiry

Time Limit:2000/1000 MS(    MemoryLimit: 65536/32768 K (
Total Submission(s):18568    AcceptedSubmission(s): 4847


Problem Description
One of the first users of BIT's newsupercomputer was Chip Diller. He extended his exploration ofpowers of 3 to go from 0 to 333 and he explored taking various sumsof those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wishTimothy were here to see these results.'' (Chip moved to a newapartment, once one became available on the third floor of theLemon Sky apartments on ThirdStreet.) 
 


Input
The input will consist of at most 100lines of text, each of which contains a single VeryLongInteger.Each VeryLongInteger will be 100 or fewer characters in length, andwill only contain digits (no VeryLongInteger will benegative). 

The final input line will contain a single zero on a line byitself.
 


Output
Your program should output the sum ofthe VeryLongIntegers given in the input. 


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blankline followed by N input blocks. Each input block is in the formatindicated in the problem description. There is a blank line betweeninput blocks.

The output format consists of N output blocks. There is a blankline between output blocks.
 


Sample Input
 
  
1123456789012345678901234 567890 123456789012345678901234 567890123456789012345678901234 567890 0
 


Sample Output
 
  
370370367037037036703703 703670
 


由于int类型长度的限制,一旦数字在-2147483648~2147483647之外,便会发生溢出,所以无法使用int类型来对大数字进行加减操作。下面是两种不同的做法:

所有函数都由自己写(事实上和C语言无异):

 

import java.util.*;
 
public classMain {
  
  public StringaddZero(String a,Stringb){//添加0到相同位数
     int i,n;
     n=a.length()-b.length();
     for(i=0;i<</span>n;i++){
        b="0"+b;
     }
     return b;
  }
  
  public String add(Stringa,Stringb){//相加
     String res="";
     int x,y,firadd=0;
     int i,temp;
     char[] ac,bc;
     x=a.length();
     y=b.length();
     if(x>y){
        b=addZero(a,b);
     }
     else
        a=addZero(b,a);
     ac=a.toCharArray();
     bc=b.toCharArray();
     for(i=ac.length-1;i>=0;i--){
        temp=Integer.parseInt(ac[i]+"")+Integer.parseInt(bc[i]+"")+firadd;
        firadd=temp/10;
        temp=temp;
        res=""+temp+res;
     }
     if(firadd!=0)
        res=firadd+res;
     return res;
  }
  
  public static void main(String[]args){
     int n,i,count=0;
     String a,b;
     String[] res=newString[100];
     Main m=newMain();
     Scanner sc=newScanner(System.in);
     n=sc.nextInt();
     if(n==0)
        System.exit(0);
     count=1;
     for(i=0;i<(n-1);i++){
        a="0";
        while(true){
           b=sc.next();
           if(b.equals("0"))
               break;
           a=m.add(a,b);
        }
        res[i]=a;
     }
     while((a=sc.nextLine()).length()==0);
     if(!a.equals("0")){
        b=sc.nextLine();
        while(!b.equals("0")){
           a=m.add(a,b);
           b=sc.nextLine();
        }
     }
     if(n==1)
        i=0;
     res[i]=a;
     for(i=0;i<</span>n;i++){
        System.out.println(res[i]);
        if(i<</span>n-1)
           System.out.println();
     }
  }
}


将大的整数以字符串的形式保存起来,然后转换成char类型数组,对长度较小的进行高位填0,使之达到相同位数。之后从低位相加,分别获取相加后个位和十位的数字,个位保存到相应的位置,十位加到高一位的位置(逻辑上高一位,下标为低一位),如此循环到逻辑最高位为止(下标最低位)。

使用java类库提供的大数类BigInteger():

 

import java.util.*;
import java.math.*;
 
public class Main {
 
        public static void main(String[] args){
                  BigInteger sum;
                  int n,i;
                  String a;
                  Scanner sc=new Scanner(System.in);
                  n=sc.nextInt();
                  for(i=0;i
                           sum=BigInteger.ZERO;
                           a=sc.next();
                           while(!a.equals("0")){
                                    sum=sum.add(new BigInteger(a));
                                    a=sc.next();
                           }
                           System.out.println(sum);
                           if(i!=(n-1))
                                    System.out.println();
                  }
                  sc.close();
        }
}

由于java.math包中已经包含了大数类BigInteger,所以事实上并不需要我们写任何算法,只需要将输入的字符串转换为BigInteger类型然后利用BigInteger中的BigInteger add(BigInteger val)方法就行了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值