懒惰是最大的敌人!笔都不愿意动,怎么能行
package com.example.myapplication.ui;
import java.util.Arrays;
public class Main {
// 写一个算法。2个大int整数相乘,结果会超过int的范围,写个算法实现大数的乘法。
// 可以用string来实现
// 不能用long等
public static void main(String[] args) {
int a = 9999;
int b = 9999;
int c = a * b;
int[] aR = new int[]{0,0,9,9,9,9};
int[] bR = new int[]{0,0,9,9,9,9};
System.out.println(c);
System.out.println(Arrays.toString(aR));
System.out.println(Arrays.toString(bR));
int[] lRTemp = null;
for (int i = bR.length - 1; i >= 0; i--) {
int[] aRTemp = new int[aR.length + (bR.length - 1)];
for (int j = aR.length - 1; j >= 0; j--) {
aRTemp[j + i] = bR[i] * aR[j];
}
for (int j = aRTemp.length - 1; j >= 0; j--) {
if (aRTemp[j] > 9) {
aRTemp[j - 1] = aRTemp[j - 1] + aRTemp[j] / 10;
aRTemp[j] = aRTemp[j] % 10;
}
}
if (lRTemp != null) {
for (int j = aRTemp.length - 1; j >= 0; j--) {
aRTemp[j] = aRTemp[j] + lRTemp[j];
if (aRTemp[j] > 9) {
aRTemp[j - 1] = aRTemp[j - 1] + aRTemp[j] / 10;
aRTemp[j] = aRTemp[j] % 10;
}
}
}
lRTemp = aRTemp;
}
System.out.println(toString(lRTemp));
}
public static String toString(int[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i] >= 10 ? a[i] : "" + a[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
}
网友评论