大数
2017-08-09 本文已影响9人
远o_O
- 大数乘法
public class BigInteger {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str1 = scanner.next();
String str2 = scanner.next();
scanner.close();
int ret[] = new int[str1.length() + str2.length()];
//72106547548473106236 982161082972751393
for(int i = str1.length() - 1; i >= 0; --i){
for(int j = str2.length() - 1; j >= 0; --j){
int num1 = str1.charAt(i) - '0';
int num2 = str2.charAt(j) - '0';
ret[i+j] += (ret[i+j+1] + num1 * num2)/10;//十位:就是上一次的个位 + 进位
ret[i+j+1] = (ret[i+j+1] + num1 * num2)%10;//个位:就是个位
}
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < ret.length; ++i){
if(i == 0 && ret[i] == 0)
continue;
builder.append(ret[i]);
}
System.out.println(builder.toString());
}
}