思路:根据提示的内容,我们可以得到c[i][j] += a[i][k]*b[k][j],k>=0&&k<s
PS:这道题本身不难,但是当我定义A[m][s] B[s][n] c[m][n] 这样的大小时,总有几个测试数据过不去,只能得42分,后来我将三个数组大小都设定了为200*200,全部数据都通过了!
1 import java.util.Scanner; 2 public class _86矩阵乘法 { 3 public static void main(String[] args) { 4 Scanner scanner = new Scanner(System.in); 5 int m = scanner.nextInt(); 6 int s = scanner.nextInt(); 7 int n = scanner.nextInt(); 8 int[][] a = new int[200][200]; 9 int[][] b = new int[200][200]; 10 for (int i = 0; i < m; i++) { 11 for (int j = 0; j < s; j++) { 12 a[i][j] = scanner.nextInt(); 13 } 14 } 15 for (int i = 0; i < s; i++) { 16 for (int j = 0; j < n; j++) { 17 b[i][j] = scanner.nextInt(); 18 } 19 } 20 int[][] c = new int[200][200]; 21 for (int i = 0; i < m; i++) { 22 for (int j = 0; j < n; j++) { 23 for (int j2 = 0; j2 < s; j2++) { 24 c[i][j] += a[i][j2]*b[j2][j]; 25 } 26 } 27 } 28 for (int i = 0; i < m; i++) { 29 for (int j = 0; j < n; j++) { 30 System.out.print(c[i][j]+" "); 31 } 32 System.out.println(); 33 } 34 } 35 }