AcWing 793. 高精度乘法
原题链接
简单
作者:
恒心
,
2021-01-23 17:46:08
,
所有人可见
,
阅读 296
import java.util.*;
import java.io.*;
class Main{
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
char[] arr = sc.nextLine().toCharArray();
List<Integer> A = new ArrayList<Integer>();
for(int i = arr.length - 1; i >= 0; --i){
A.add(arr[i] - '0');
}
int b = sc.nextInt();
List<Integer> C = mul(A, b);
for(int i = C.size() - 1; i >= 0; --i){
System.out.print(C.get(i));
}
}
public static List<Integer> mul(List<Integer> A, int b){
List<Integer> C = new ArrayList<Integer>();
int t = 0;
// 判断处理t > 0
for(int i = 0; (i < A.size())||(t > 0); ++i){
if(i < A.size()) t += (A.get(i) * b);
C.add(t % 10);
t /= 10;
}
// trim '0' element
while(C.size() > 1 && C.get(C.size() - 1) == 0){C.remove(C.size() - 1);};
return C;
}
}