Java 处理大数运算,超过int long 之类的

来源:互联网 发布:软件架构模式 pdf 编辑:程序博客网 时间:2024/05/21 02:35

一般在做acm题的时候经常遇到,两个超级大数的相加相减之类的问题,比如:

杭电acm上的1002

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 270431    Accepted Submission(s): 52217


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
21 2112233445566778899 998877665544332211
 

Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
 

Author
Ignatius.L
 


我之前在书上看的思路有,用一个数组来存储这个大数的每一位,然后自定义关于数组的加减运算,自己控制进位什么的。


完全自己实现的代码:


import java.util.Scanner;public class _1002 {static char[] add(String x,String y){//自定义String数组加法运算char[] result = new char[10000];String a,b;if(x.length()>y.length()){a = x;b = y;}else {a = y;b = x;}int carry = 0;int i = 0;for(;i<a.length() ;i++){if (i<b.length()) {int tmpa = a.charAt(i)-'0';int tmpb = b.charAt(i)-'0';int loc = tmpa + tmpb + carry;result[i] = (char) ((loc % 10)+'0');//这里的加减'0'是为了把(char)字符的阿拉伯数字转化为(int)整数的阿拉伯数字if (loc > 9)carry = 1;elsecarry = 0;}else{int tmpa = a.charAt(i)-'0';int tmpb = 0;int loc = tmpa + tmpb + carry;result[i] = (char) ((loc % 10)+'0');if (loc > 9)carry = 1;elsecarry = 0;}}if(carry == 1)result[i] =(1+'0');return result;}static int count(char[] aa ){//为了取得实际的数组长度,因为如果你用xxx.length取得开辟的空间,不是实际的存储大小int i = 0;int j = 0;while(true){if(aa[i++] != 0)j++;elsebreak;}return j;}static String reverse1(String s) {//反转字符数组,因为存储的时候大数的最低位在s[0],运算从低位向高位,从左向右进行,倒过来符合人的习惯。  char[] array = s.toCharArray();  String reverse = "";  for (int i = array.length - 1; i >= 0; i--){    reverse += array[i];  }  return reverse; }static String reverse1(char[] array) {  String reverse = "";  for (int i = count(array) -1 ; i >= 0; i--)   reverse += array[i];  return reverse; }public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int i = 0;while(i<N){String x =  new String(sc.next());String y = new String(sc.next());String st1 = reverse1(x);//把大数存储为String型String st2 = reverse1(y);char[] st3 = add(st1,st2);String out = reverse1(st3);System.out.println("Case "+ ++i +":");System.out.println(x+" + "+y+" = "+out);if(i!=N)/**************这真坑爹,题意是最后一行没有空格,如果直接写上system.out.println()所有用例测试的最后一行还是有空格的,所以<span style="white-space:pre"></span>是表达错误*************/System.out.println();}}}





——————————————————————————以上都是自己手动实现,其实可以用jdk提供的一个BigInteger,方便快捷————————————


代码简化为



import java.util.Scanner;  import java.math.BigInteger;    public class _1002_BigInt_study  {      public static void main(String[] args)  {          Scanner keyIn = new Scanner(System.in);          int count = 0;               count = keyIn.nextInt();              for(int i =0;i<count;i++){              BigInteger a= keyIn.nextBigInteger();              BigInteger b= keyIn.nextBigInteger();              BigInteger sum;              sum = a.add(b);              System.out.println("Case "+(i+1)+":");              System.out.println(a+" + "+b+" = "+sum);              if(i!=count-1)                System.out.println();           }              }  }  


//从实际的时间空间使用度上来说我自己写的更加的好一些,但是普适性较差,jdk提供的当然普适性强,但是针对性较差。

//我花了一上午的功夫,biginteger一下就解决了,还是得多读书。

//希望对大家有帮助。




0 0
原创粉丝点击