【筆試題】拼多多2018校招內推程式設計
2、大資料相乘
問題描述
有兩個用字串表示的非常大的大整數,算出他們的乘積,也是用字串表示。不能用系統自帶的大整數型別。
輸入描述:
空格分隔的兩個字串,代表輸入的兩個大整數
輸出描述:
輸入的乘積,用字串表
問題分析
只要把加法換成乘法就好,半夜做題傷腦子啊,頭疼。。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] number = str.split(" ");
int lengh = number[0].length() + number[1].length() - 1;
int[] save = new int[lengh];
int height = number[1].length();
int point = number[1].length() - 1;
for (int i = 0; i < height; i++) {
int last = Integer.parseInt(number[1].charAt(point) + "");
int depoint = 0;
point--;
for (int j = number[0].length() - 1; j >= 0; j--) {
int first = Integer.parseInt(number[0].charAt(j) + "");
save[lengh - 1 - depoint - i] = first * last + save[lengh - 1 - depoint - i];
depoint++;
}
}
for (int i = save.length - 1; i > 0; i--) {
save[i - 1] = save[i - 1] + save[i] / 10;
save[i] = save[i] % 10;
}
for (int i=0;i<save.length;i++){
System.out.print(save[i]);
}
}
}