藍橋杯 演算法訓練 矩陣乘法
阿新 • • 發佈:2019-01-27
import java.util.Scanner;
public class Main {
public static void cheng(int[][] arr,int[][] brr,int m,int n,int s) {
int[][] crr = new int[m][s];
for(int i = 0;i < m;i ++) {
for(int j = 0;j < s;j ++) {
int result = 0;
for(int time = 0 ; time < n;time ++) {
result += arr[i][time] * brr[time][j];
}
crr[i][j] = result;
}
}
for(int i = 0;i < m;i ++) {
for(int j = 0;j < s;j ++) {
if(j == s -1) {
System.out .print(crr[i][j]);
}else {
System.out.print(crr[i][j]+" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int s = sc.nextInt();
int[][] arr = new int[m][n];
int[][] brr = new int[n][s];
for(int i = 0;i < m;i ++) {
for(int j = 0;j < n;j++) {
arr[i][j] = sc.nextInt();
}
}
for(int i = 0;i < n;i++) {
for(int j = 0;j < s;j++) {
brr[i][j] = sc.nextInt();
}
}
cheng(arr,brr,m,n,s);
}
}