PS:這個不是本身寫的,測試OK,供參考。數組
/**
* 大數相乘
*/app
1 public class BigData { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 StringBuffer sb = new StringBuffer(); 6 // String str1 = "99999999999"; 7 // String str2 = "99999999999"; 8 for (int i = 0; i < 2000; i++) { 9 sb.append(9); 10 } 11 String str1 = sb.toString(); 12 String str2 = str1; 13 BigData test = new BigData(); 14 String result = test.mutiStr(str1, str2); 15 System.out.println(result); 16 } 17 18 public String mutiStr(String str1, String str2) { 19 String result = null;// 存放結果字符串 20 21 // 肯定負號 22 int flagStr1 = 0; 23 if (str1.startsWith("-")) { 24 flagStr1++; 25 } 26 int flagStr2 = 0; 27 if (str2.startsWith("-")) { 28 flagStr2++; 29 } 30 // ********************************************************************* 31 32 // 將數字放到整數數組,逆序,字符串尾部對齊 33 int[] arrStr1 = new int[str1.length() - flagStr1]; 34 for (int i = 0; i < arrStr1.length; i++) { 35 arrStr1[i] = str1.charAt(str1.length() - 1 - i) - '0'; 36 } 37 int[] arrStr2 = new int[str2.length() - flagStr2]; 38 for (int i = 0; i < arrStr2.length; i++) { 39 arrStr2[i] = str2.charAt(str2.length() - 1 - i) - '0'; 40 } 41 // *********************************************************************** 42 43 // 運算開始,arrStr2的每位去乘arrStr1的每位 44 StringBuffer subResult = new StringBuffer();// 中間結果 45 for (int i = 0; i < arrStr2.length; i++) { 46 StringBuffer midResult = new StringBuffer();// 每位結果 47 int subBenWei = 0;// 每位相乘時的本位上數字 48 int subJinWei = 0;// 每位相乘時的進位上數字 49 for (int k = 0; k < i; k++) { 50 midResult.append(0);// 先添加0,確保每位末尾對齊,便於加法運算 51 } 52 for (int j = 0; j < arrStr1.length; j++) { 53 subBenWei = (arrStr2[i] * arrStr1[j] + subJinWei) % 10; 54 subJinWei = (arrStr2[i] * arrStr1[j] + subJinWei) / 10; 55 midResult.append(subBenWei); 56 if ((j == arrStr1.length - 1) && subJinWei > 0) { 57 midResult.append(subJinWei);// 若是最前面位相乘有進位,結果添加進位數字 58 } 59 } 60 if (0 == i) { 61 subResult = midResult;// 第一位,子結果存放第一位結果 62 } else { 63 // 各位對齊,確保中間結果和每位結果位數相同,便於相加運算 64 if (midResult.length() > subResult.length()) { 65 int diff = midResult.length() - subResult.length(); 66 while (diff > 0) { 67 subResult.append(0); 68 diff--; 69 } 70 } else { 71 int diff = subResult.length() - midResult.length(); 72 while (diff > 0) { 73 midResult.append(0); 74 diff--; 75 } 76 } 77 // ********************************************************* 78 // 相加運算,結果放到subResult中 79 int benWei = 0; 80 int jinWei = 0; 81 for (int j = 0; j < subResult.length(); j++) { 82 83 benWei = ((subResult.charAt(j) - '0') 84 + (midResult.charAt(j) - '0') + jinWei) % 10; 85 jinWei = ((subResult.charAt(j) - '0') 86 + (midResult.charAt(j) - '0') + jinWei) / 10; 87 subResult.setCharAt(j, (char) (benWei + '0')); 88 } 89 // 若是最後相加任有進位,結果中添加進位 90 if (jinWei > 0) { 91 subResult.append(jinWei); 92 } 93 // *********************************************************** 94 } 95 } 96 // 判斷「-」的個數,沒有或有兩個則爲結果爲正,不考慮,如只有一個則結果要添加「-」 97 if (1 == (flagStr1 + flagStr2)) { 98 subResult.append('-'); 99 } 100 result = subResult.reverse().toString();// 最終子結果逆序,即爲結果 101 return result; 102 } 103 }