解題的思路並不複雜:模擬算數的乘法運算過程
可是一次直接AC仍是比較困難的。
注意點:
1.進位 每一輪的進位最終存在的話,還須要加上
2.高位和低位的存儲問題
3.輸入,輸出問題
輸出ACM,採用System,out.println();java
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); } }