AcWing 793. 高精度乘法Java
原题链接
简单
作者:
还想听你的故事
,
2021-03-24 10:46:49
,
所有人可见
,
阅读 304
高精度乘法
import java.math.BigInteger;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
int res[]=new int[s1.length()+s2.length()];
for(int i=s1.length()-1;i>=0;i--){
int x=s1.charAt(i)-'0';
for(int j=s2.length()-1;j>=0;j--){
int y=s2.charAt(j)-'0';
int s=x*y+res[i+j+1];
res[i+j]+=s/10;
res[i+j+1]=s%10;
}
}
StringBuilder sb=new StringBuilder();
int k=0;
while(k<res.length&&res[k]==0) k++;
if(k==res.length) System.out.println(0);
else{
while(k<res.length) sb.append(res[k++]);
System.out.println(sb.toString());
}
}
}
BigInteger
import java.math.BigInteger;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
BigInteger a=new BigInteger(s1);
BigInteger b=new BigInteger(s2);
System.out.println(a.multiply(b).toString());
}
}