牛客2018校招 1. 拼多多 大整数相乘
解题的思路并不复杂:模拟算数的乘法运算过程
但是一次直接AC还是比较困难的。
注意点:
1.进位 每一轮的进位最终存在的话,还需要加上
2.高位和低位的存储问题
3.输入,输出问题
输出ACM,采用System,out.println();
import java.io.*;
import java.util.*;
public class Main{
public static String mul(String s1, String s2){
int[] i1 = new int[s1.length()];
int[] i2 = new int[s2.length()];
int[] res = new int[s1.length() + s2.length()];
for (int i = 0; i < s1.length(); i++)
i1[i] = s1.charAt(i) - '0';
for (int i = 0;i < s2.length() ;i++)
i2[i] = s2.charAt(i) - '0';
int ji = 0;
int k = i2.length;
int i = 0, j = 0;
for (i = i2.length - 1;i >= 0 ; i--){
for(j = i1.length - 1; j >= 0; j--){
res[k + j] += (i2[i]* i1[j]) % 10 + ji;
ji = (i2[i]* i1[j]) / 10;
if (res[k + j] >= 10){
res[k + j - 1] += res[k + j] / 10;
res[k + j] = res[k + j] % 10;
}
}
if (ji > 0) res[k + j] += ji;
ji = 0;
k --;
}
boolean b = false;
String sr = "";
for (i = 0; i < res.length; i ++)
if (b)
sr += String.valueOf(res[i]);
else
if (res[i] != 0){
b = true;
sr += String.valueOf(res[i]);
}
return sr;
}
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
String res = mul(input[0], input[1]);
System.out.println(res);
}
}