请编写程序实现矩阵的乘法
输入的第一行包括3个数字 ,为 m,p,n接下来的m行数据为第一个矩阵的数据,每个数据之间用英文逗号,分割接下来的p行数据为第二个矩阵的数据,每个数据之间用英文逗号,分割
请输出矩阵相乘的结果,一行内的数据用英文逗号,拼接
2,3,2 1,0,-1 1,1,-3 0,3 1,2 3,1
-3,2 -8,2
请注意字符分割和拼接都使用英文逗号 , 输入和输出前后没有空行
import java.util.*; public class Main { private static int[][] matrixMuilty(int[][] a, int[][] b) { int m = a.length, p = b.length, n = b[0].length; int[][] res = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < p; k++) { res[i][j] += a[i][k] * b[k][j]; } } } return res; } //输入三个整数,分别表示:第一个矩阵的 行 列【也就是第二个矩阵的行】 第二个矩阵的列 public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String[] ss = s.split(","); int m = Integer.parseInt(ss[0]), p = Integer.parseInt(ss[1]), n = Integer.parseInt(ss[2]); if (m == 0 || p == 0 || n == 0) { sc.close(); return; } int[][] a = new int[m][p]; int[][] b = new int[p][n]; for (int i = 0; i < m; i++) { s = sc.nextLine(); ss = s.split(","); for (int j = 0; j < p; j++) { a[i][j] = Integer.parseInt(ss[j]); } } for (int i = 0; i < p; i++) { s = sc.nextLine(); ss = s.split(","); for (int j = 0; j < n; j++) { b[i][j] = Integer.parseInt(ss[j]); } } sc.close(); int[][] res = matrixMuilty(a, b); for (int i = 0; i < m; i++) { StringBuilder sb = new StringBuilder(); for (int j = 0; j < n; j++) { sb.append(res[i][j]).append(","); } System.out.println(sb.substring(0, sb.length() - 1)); } } }
#include<bits/stdc++.h> int main() { int m, p, n,a[100][100] = { 0 }, b[100][100] = { 0 }, c[100][100] = {0}; while(scanf("%d,%d,%d",&m,&p,&n)!=EOF) { for (int i = 0; i < m; i++) for (int j = 0; j < p; j++) scanf("%d%*c", &a[i][j]); for (int i = 0; i < p; i++) for (int j = 0; j < n; j++) scanf("%d%*c", &b[i][j]); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) for (int k = 0; k < p; k++) c[i][j] += a[i][k] * b[k][j]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d", c[i][j]); if (j != n - 1) printf(","); else printf("\n"); } } } }
def dot_product(x, y): sum = 0 for i in range(len(x)): sum += x[i] * y[i] return sum def transpose(x): i = len(x) j = len(x[0]) res = [] for k in range(j): temp = [] for ele in x: temp.append(ele[k]) res.append(temp) return res while True: try: s = list(map(int,input().split(','))) x,y,z = s[0],s[1],s[2] A = [] B = [] for i in range(x): A.append(list(map(int, input().split(',')))) for j in range(y): B.append(list(map(int, input().split(',')))) for k in range(x): result = [] for l in range(len(transpose(B))): result.append(dot_product(A[k], transpose(B)[l])) word = ','.join(list(map(str, result))) print(word) except: break
public static int[][] matrixMultiplication(int[][] A, int[][] B) throws Exception { if (A != null && B != null) { if (A[0].length != B.length) { throw new Exception("A的列数和B的行数不相等,请确认后再尝试计算"); } int[][] C = new int[A.length][B[0].length]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < B[i].length; j++) { int k = 0; C[i][j] = 0; while (k < A[i].length) { C[i][j] += A[i][k] * B[k][j]; k++; } } } return C; } return null; }