java程序计算两个大整数相乘

方法1 :用两个字符串保存输入的大整数,然后用第二个字符串的每一位去乘第一个字符串的数字值,最后将每次的结果错位相加即可。时间复杂度高O(n^2)

方法2:将两个大整数X,Y每次分割成两半,第一个分割成AB,第二个分割成CD。所以最后结果XY=(A*10^n/2 +B)(C*10^m/2+D);进行分解可得

XY=AC*10^(n+m)/2+AD*10^n/2 +BC*10^m/2 +BD; 注意n/2,m/2为字符串后半度的位数。分治法的时间复杂度为O(nlogn);


代码如下:两个方法 放在同名的重载函数中。

package com.test; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n, m; String str[] = in.nextLine().split(" "); String result = multiBigInt(str[0], str[1]); // 除去首位的0 char[] temp = result.toCharArray(); int i = 0; for (; i < temp.length; i++) { if (temp[i] == '0') continue; else break; } System.out.println(result.substring(i, result.length())); String result2 = ""; result2 = multiBigInt(str[0].toCharArray(), str[1].toCharArray()); temp = result2.toCharArray(); i = 0; for (; i < temp.length; i++) { if (temp[i] == '0') continue; else break; } System.out.println(result2.substring(i, result2.length())); in.close(); }  //计算单个字符 乘 一个字符串 public static String multiBigIntSingle(char[] a, char b) { int pre = 0; String result = ""; for (int j = a.length - 1; j >= 0; j--) { int temp = a[j] - '0'; int tempb = b - '0'; int sum = temp * tempb + pre; pre = sum / 10; int left = sum % 10; result += left; } if (pre != 0) result += pre; char[] sb = result.toCharArray(); String value = ""; for (int j = sb.length - 1; j >= 0; j--) value += sb[j]; return value; }  //方法1 public static String multiBigInt(char[] a, char[] b) { String c = ""; // 保存每一行相加后的结果 int j = 0; // 控制错位 for (int i = b.length - 1; i >= 0; i--) { c = addBigInt(c.toCharArray(), multiBigIntSingle(a, b[i]).toCharArray(), j++); } return c; }  //方法二 public static String multiBigInt(String a, String b) { if (a.length() == 1) return multiBigIntSingle(b.toCharArray(), a.charAt(0)); if (b.length() == 1) return multiBigIntSingle(a.toCharArray(), b.charAt(0)); int mid1 = a.length() / 2; int mid2 = b.length() / 2; String A = a.substring(0, mid1); String B = a.substring(mid1, a.length()); String C = b.substring(0, mid2); String D = b.substring(mid2, b.length()); String AC = multiBigInt(A, C); for (int i = 0; i < a.length() - mid1 + b.length() - mid2; i++) { AC += '0'; } String AD = multiBigInt(A, D); for (int i = 0; i < a.length() - mid1; i++) { AD += '0'; } String CB = multiBigInt(C, B); for (int i = 0; i < b.length() - mid2; i++) { CB += '0'; } String BD = multiBigInt(B, D); String result = addBigInt(AC.toCharArray(), CB.toCharArray(), 0); result = addBigInt(result.toCharArray(), AD.toCharArray(), 0); result = addBigInt(result.toCharArray(), BD.toCharArray(), 0); return result; }  //将两个大整数相加 len用于控制错位相加 public static String addBigInt(char[] a, char[] b, int len) { int maxlen = a.length + b.length; char[] revA = reverse(a); char[] revB = reverse(b); String sb = ""; int tempa = 0; int tempb = 0; int pre = 0; for (int i = 0; i < maxlen; i++) { tempa = 0; tempb = 0; if (i < revA.length) tempa = revA[i] - '0'; // 第二行要先以为len的距离 ,错位相加 if (i < revB.length + len && i >= len) tempb = revB[i - len] - '0'; int sum = tempa + tempb + pre; pre = sum / 10; int left = sum % 10; sb += left; } while (pre != 0) { sb += pre % 10; pre /= 10; } char[] result = sb.toCharArray(); String value = ""; for (int j = result.length - 1; j >= 0; j--) value += result[j]; return value; }  //将一个字符数组反转,便于整数相加 public static char[] reverse(char[] a) { char[] b = new char[a.length]; int i = 0; int j = a.length - 1; for (; j >= 0; j--) { b[i] = a[j]; i++; } return b; } }
987654321 45 44444444445 44444444445
987654321 987654321 975461057789971041 975461057789971041 
7415863 9874563210 73228407950200230 73228407950200230 


计算结果用计算机检验正确。

    原文作者:大整数乘法问题
    原文地址: https://blog.csdn.net/qq_32250495/article/details/76576602
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞