# 大整数相乘(JAVA模拟）

## 题目

72106547548473106236 982161082972751393

70820244829634538040848656466105986748

## 代码

import java.util.*;
import java.io.*;
import java.math.*;

public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String x,y,ans;
while(in.hasNext()) {
x=in.next();
y=in.next();
ans=mul(x,y);
System.out.println(ans);
}

}

public static String mul(String x,String y){
if(x.charAt(0)=='-' && y.charAt(0) =='-') return _mul(x.substring(1),y.substring(1));
if(x.charAt(0)=='-' && y.charAt(0)!='-') return _mul(x.substring(1),y);
if(x.charAt(0)!='-' && y.charAt(0)!='-') return _mul(x,y);
if(x.charAt(0)!='-' && y.charAt(0)=='-') return _mul(x,y.substring(1));
return "0";
}

public static String _mul(String x,String y){
String ans="0";
int cnt=0;
y=new StringBuffer(y).reverse().toString();
while(true){
if(x.length()==0) break;
char ch=x.charAt(x.length()-1);
int carry=0;
String tmp="";
for(int i=0;i<y.length();i++){
carry=carry+(y.charAt(i)-'0')*(ch-'0');
tmp+=""+carry%10;
carry/=10;
}
tmp+=carry;
tmp=new StringBuffer(tmp).reverse().toString();

for(int i=0;i<cnt;i++){
tmp+="0";
}
cnt++;

x=x.substring(0,x.length()-1);
}
if(ans.equals("")) ans="0";
return ans;
}

public static String add(String x,String y){
int k=0;
while(k<x.length() && x.charAt(k)=='0') k++;
x=x.substring(k);
k=0;
while(k<y.length() && y.charAt(k)=='0') k++;
y=y.substring(k);

x=new StringBuffer(x).reverse().toString();
y=new StringBuffer(y).reverse().toString();

String ans="";
int carry=0,i,j;
for(i=0,j=0;i<x.length()&&j<y.length();i++,j++){
carry+=(x.charAt(i)-'0')+(y.charAt(j)-'0');
ans+=carry%10;
carry/=10;
}

for(;i<x.length();i++){
carry+=x.charAt(i)-'0';
ans+=carry%10;
carry/=10;
}

for(;j<y.length();j++){
carry+=y.charAt(j)-'0';
ans+=carry%10;
carry/=10;
}

ans+=carry;
ans=new StringBuffer(ans).reverse().toString();
k=0;
while(k<ans.length() && ans.charAt(k)=='0') k++;
ans=ans.substring(k);
if(ans.equals("")) ans="0";
return ans;
}
}


## 心得

==是判断是否是同一对象，equals是判断内容是否相同。

04-02 1万+

03-27 1520
05-08 796
10-04 1685
09-08 210
08-02
08-02 105
08-22 2882
09-02 1万+