两个超大数的加法-遇到的一个笔试题
最近碰到的一个笔试题,应该有多种实现方式,我也只是那一个最常用的方法搞定的
1 import java.math.BigInteger; 2 import java.util.regex.Pattern; 3 4 public class AddBigData { 5 6 private static final int SINGLE_MAX = 9; 7 private static final int ONE = 1; 8 9 public static String addBigData(String fir, String sec) throws NumberFormatException{ 10 //check format is ok, first to check weather exist zero 11 if("0".equals(fir)){ 12 return sec; 13 }else if("0".equals(sec)){ 14 return fir; 15 }else { 16 //use regx to check number format 17 Pattern dataPattern = Pattern.compile("^[1-9]\\d*$");//start not with zero ,end with data 18 boolean firCheck = dataPattern.matcher(fir).find(); 19 boolean secCheck = dataPattern.matcher(sec).find(); 20 21 if(firCheck && secCheck){ 22 //begin to cal 23 return add(fir, sec); 24 }else{ 25 //throw exception 26 throw new NumberFormatException("输入的字符串格式错误!请输入正确的数字。"); 27 } 28 } 29 } 30 31 public static String add(String fir, String sec) { 32 StringBuilder res = new StringBuilder(); 33 34 char[] firs = fir.toCharArray(); 35 char[] secs = sec.toCharArray(); 36 //get min length to cal, get maxlen to store 37 int minLen = Math.min(firs.length, secs.length); 38 int maxLen = Math.max(firs.length, secs.length); 39 40 int[] aiRes = new int[maxLen+1];//to store, be carefull the aiRes's length must be maxLen+1 41 //init aiRes 42 aiRes[0] = 0; 43 char[] maxString = firs.length >= secs.length ? firs : secs; 44 for(int i = 0; i < (maxLen - minLen); i++){ 45 aiRes[i+1] = Integer.parseInt(String.valueOf(maxString[i])); 46 } 47 48 int prompt = 0;//data to add in the next step 49 //begin to cal 50 for(int i = 0; i < minLen; i++){ 51 char cFir = firs[firs.length - 1 - i]; 52 char cSec = secs[secs.length - 1 - i]; 53 //change to number and add between (max-min) to max-1 54 int iFir = Integer.parseInt(String.valueOf(cFir)); 55 int iSec = Integer.parseInt(String.valueOf(cSec)); 56 57 iFir += iSec; 58 iFir += prompt; 59 aiRes[aiRes.length - 1 - i] = iFir % 10; 60 prompt = iFir / 10; 61 } 62 aiRes[maxLen - minLen] += prompt; 63 //is needed to cal continue 64 if(aiRes[maxLen - minLen] > SINGLE_MAX){ 65 aiRes[maxLen - minLen] %= 10; 66 //cal the last of aiRes between 0 to max-min-1 67 for(int i = maxLen - minLen - 1; i >= 0; i--){ 68 aiRes[i] += ONE; 69 if (aiRes[i] > SINGLE_MAX) { 70 aiRes[i] %= 10; 71 }else{ 72 break; 73 } 74 } 75 } 76 77 if(aiRes[0] > 0){ 78 res.append(aiRes[0]); 79 } 80 for(int i = 1; i < aiRes.length; i++){ 81 res.append(aiRes[i]); 82 } 83 84 return res.toString(); 85 } 86 87 /** 88 * @param args 89 */ 90 public static void main(String[] args) { 91 String firstBigData = "19911999998888888888888888888888888888888888888888999999999999999999999999999999"; 92 String secBigData = "1008000001111111111111111111111111111111111111111000000000000000000000000000001"; 93 System.out.println(addBigData(firstBigData, secBigData)); 94 BigInteger bi1 = new BigInteger(firstBigData); 95 BigInteger bi2 = new BigInteger(secBigData); 96 bi1 = bi1.add(bi2); 97 System.out.println(bi1.toString()); 98 /*result: 20920000000000000000000000000000000000000000000000000000000000000000000000000000*/ 99 } 100 101 }