最近在准备面试,遇到很多大数据运算相关的面试题,这里做个记录。
一、两个非负整数相乘
解题思路借鉴于:qwurey-Java实现大整数相乘
import java.util.Scanner;
public class BigNumMultiply {
static int N = 1000;
static int a[] = new int[N];
static int b[] = new int[N];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s1 = scanner.nextLine().trim();
String s2 = scanner.nextLine().trim();
int len1 = s1.length();
int len2 = s2.length();
getDigit(s1, a);
getDigit(s2, b);
doOutput(doMultiply(a, len1, b, len2));
}
public static void doOutput(int []arr) {
boolean findFirstNum = false;
for (int i = arr.length - 1; i >= 0; i--) {
if (!findFirstNum && arr[i] == 0) {
continue;
}
findFirstNum = true;
System.out.print(arr[i]);
}
System.out.println("");
}
public static int[] doMultiply(int []a, int len1, int []b, int len2) {
int res[] = new int[2 * N + 1];
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
int temp = res[j + i] + a[i] * b[j];
int n = temp % 10;
int m = temp / 10;
res[i + j] = n;
res[i + j + 1] += m;
}
}
return res;
}
public static void getDigit(String s, int[] a) {
int len = s.length();
for (int i = 0; i < len; i++) {
a[len - 1 - i] = s.charAt(i) - '0';
}
}
}