如果A是个x行y列的矩阵,B是个y行z列的矩阵,把A和B相乘,其结果将是另一个x行z列的矩阵C。这个矩阵的每个元素是由下面的公式决定的
矩阵的大小不超过100*100
第一行包含一个正整数x,代表第一个矩阵的行数
第二行包含一个正整数y,代表第一个矩阵的列数和第二个矩阵的行数
第三行包含一个正整数z,代表第二个矩阵的列数
之后x行,每行y个整数,代表第一个矩阵的值
之后y行,每行z个整数,代表第二个矩阵的值
对于每组输入数据,输出x行,每行z个整数,代表两个矩阵相乘的结果
2 3 2 1 2 3 3 2 1 1 2 2 1 3 3
14 13 10 11
1 2 3 3 2 1 乘以 1 2 2 1 3 3 等于 14 13 10 11
16 8 7 17 19 16 19 14 1 14 9 7 2 7 9 16 14 16 12 13 3 3 17 5 9 8 16 1 14 16 10 13 13 14 1 13 13 15 4 7 2 6 16 16 15 5 5 15 13 1 11 11 5 0 16 14 7 7 15 0 16 4 7 16 6 0 15 2 14 11 2 17 17 5 12 8 13 11 10 1 17 10 8 15 16 17 15 7 8 13 14 5 19 11 3 11 14 5 4 9 16 13 11 15 18 0 3 15 3 19 9 5 14 12 3 9 8 7 11 18 19 14 18 12 19 9 1 0 18 17 10 5 18 16 19 6 12 5 1 17 1 5 9 16 3 14 16 4 0 19 3 6 11 9 15 18 11 17 13 5 5 19 3 16 1 12 12 13 19 1 10 5 18 19 18 6 18 19 12 3 15 11 6 5 10 17 19
1020 1490 1063 1100 1376 1219 884 966 1035 1015 715 1112 772 920 822 948 888 816 831 920 863 855 1099 828 578 1160 717 724 745 1076 644 595 930 838 688 635 1051 970 600 880 811 846 748 879 952 772 864 872 878 526 722 645 335 763 688 748 764 996 868 362 1026 681 897 836 1125 785 637 940 849 775 1082 1476 996 968 1301 1183 953 609 987 717 401 894 657 662 700 1083 1022 527 1016 746 875 909 1162 905 722 1055 708 720 1126 1296 1240 824 1304 1031 1196 905 1342 766 715 1028 956 749
# 假设两个矩阵,A是3*4的,B是4*5的,结果是3*5的 #A=[[1,2, 3, 4], # [5,6, 7, 8], # [9,10,11,12]] #B=[[1,2,3,4,5], # [4,5,6,7,8], # [7,8,9,10,11], # [10,11,12,13,14]] while True: try: x=int(input())#第一个矩阵的行数#3 y=int(input())#第一个矩阵的列数和第二个矩阵的行数#4 z=int(input())#第二个矩阵的列数#5 A=[]#矩阵A初始化 B=[]#矩阵B初始化 for i in range(x):#矩阵A赋值 li=input().split() li=[int(v) for v in li] A.append(li) for j in range(y):#矩阵B赋值 li=input().split() li=[int(v) for v in li] B.append(li) ans=[[0 for i in range(z)] for j in range(x)]#结果初始化3*5 for i in range(x): for j in range(z): for k in range(y): ans[i][j]+=A[i][k]*B[k][j] for i in range(x): for j in range(z): if j==(z-1): print(ans[i][j]) else: print(ans[i][j],end=' ') except: break
import java.util.*; public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ //获取行,列信息 int row1 = scan.nextInt(); int column = scan.nextInt(); int column2 = scan.nextInt(); int[][] first = new int[row1][column]; int[][] second = new int[column][column2]; int[][] temp = new int[row1][column2]; //将数组存储在数组中 for(int i=0;i<row1;i++){ for(int j=0;j<column;j++){ first[i][j]=scan.nextInt(); } } for(int i=0;i<column;i++){ for(int j =0;j<column2;j++){ second[i][j]=scan.nextInt(); } } //对数组进行计算 for(int i=0;i<row1;i++){ for(int j=0;j<column2;j++){ for(int k= 0;k<column;k++){ temp[i][j] += first[i][k]*second[k][j]; } } } //按照格式输出 for(int i=0;i<row1;i++){ StringBuilder sb = new StringBuilder(); for(int j=0;j<column2;j++){ if(j==column2-1){ sb.append(temp[i][j]); }else{ sb.append(temp[i][j]+" "); } } System.out.println(sb.toString()); } } } }
import java.util.*; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); int[][] m1 = new int[x][y]; int[][] m2 = new int[y][z]; int[][] r = new int[x][z]; for(int i=0; i<x; i++) { for(int j=0; j<y; j++) { m1[i][j] = sc.nextInt(); } } for(int i=0; i<y; i++) { for(int j=0; j<z; j++) { m2[i][j] = sc.nextInt(); } } for(int i=0; i<x; i++) { for(int j=0; j<z; j++) { for(int k=0; k<y; k++) { r[i][j] = r[i][j]+m1[i][k]*m2[k][j]; } } } for(int i=0; i<x; i++) { for(int j=0; j<z; j++) { System.out.print(r[i][j] + " "); } System.out.println(); } } } }
package HUAWEI2; import java.util.Scanner; /** * 矩阵乘法 * @author Administrator * */ public class Demo29 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int x1 = sc.nextInt(); int x2 = sc.nextInt(); int x3 = sc.nextInt(); int[][] a = new int[x1][x2]; int[][] b = new int[x2][x3]; for(int i=0;i<x1;i++){ for(int j=0;j<x2;j++){ a[i][j]=sc.nextInt(); } } for(int i=0;i<x2;i++){ for(int j=0;j<x3;j++){ b[i][j]=sc.nextInt(); } } calculate(a,b,x1,x2,x3); } } public static void calculate(int[][] a,int[][] b,int x1,int x2,int x3){ int[][] c = new int[x1][x3]; for(int i=0;i<x1;i++){ for(int j=0;j<x3;j++){ c[i][j]=0; for(int k=0;k<x2;k++){ c[i][j]+=a[i][k]*b[k][j]; } } } for(int i=0;i<x1;i++){ for(int j=0;j<x3;j++){ System.out.print(c[i][j]); if(j!=x3-1){ System.out.print(" "); } } System.out.println(); } } }
#include <iostream> #include <vector> using namespace std; int main() { int row1, col1, col2; while (cin >> row1 >> col1 >> col2) { vector<vector<int> > array1(row1, vector<int>(col1, 0)); vector<vector<int> > array2(col1, vector<int>(col2, 0)); vector<vector<int> > array3(row1, vector<int>(col2, 0)); for (int i = 0; i < row1; i++) for (int j = 0; j < col1; j++) { cin >> array1[i][j]; } for (int i = 0; i < col1; i++) for (int j = 0; j < col2; j++) { cin >> array2[i][j]; } for (int i = 0; i < row1; i++) { for (int j = 0; j < col2; j++) { int sum = 0; for (int k = 0; k < col1; k++) { sum += array1[i][k] * array2[k][j]; } array3[i][j] = sum; } } for (int i = 0; i < row1; i++) { for (int k = 0; k < col2 - 1; k++) cout << array3[i][k] << ' '; cout << array3[i][col2-1] << endl; } } }
#include<stdio.h> int main(){ int matrix1[100][100]={0},matrix2[100][100]={0},matrix3[100][100]={0}; int m=0,n=0,p=0; while(~scanf("%d\n%d\n%d",&m,&n,&p)){ for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ scanf("%d",&matrix1[i][j]); //读入矩阵1 } } for(int i=0;i<n;i++){ for(int j=0;j<p;j++){ scanf("%d",&matrix2[i][j]); //读入矩阵2 } } for(int i=0;i<m;i++){ for(int j=0;j<p;j++){ for(int k=0;k<n;k++){ matrix3[i][j] += matrix1[i][k]*matrix2[k][j]; //计算矩阵3的元素 } } } for(int s=0;s<m;s++){ for(int t=0;t<p;t++){ printf("%d",matrix3[s][t]); if(t!=p-1) printf(" "); //控制空格符数量 }printf("\n"); //控制换行 } } }
while (line = readline()) { // 读取数据 const x = parseInt(line, 10); const y = parseInt(readline()); const z = parseInt(readline()); const xyMatrix = []; const yzMatrix = []; const zyMatrix = []; for (let i = 0; i < x; i++) { xyMatrix[i] = readline().split(" ").map(Number); } for (let i = 0; i < y; i++) { yzMatrix[i] = readline().split(" ").map(Number); } for (let i = 0; i < z; i++) { const row = []; for (let j = 0; j < y; j++) { row.push(yzMatrix[j][i]) } zyMatrix.push(row); } // 计算矩阵相乘的结果 for (let i = 0; i < x; i++) { const row = []; for (let j = 0; j < z; j++) { const product = getProduct(xyMatrix[i], zyMatrix[j]); row.push(product); } console.log(...row); } } function getProduct(row, column) { let result = 0; for (let i = 0; i < row.length; i++) { result = result + (row[i] * column[i]); } return result; }
#include<bits/stdc++.h> using namespace std; int main() { int x, y, z; while (cin >> x >> y >> z) { vector<vector<int>> a(x + 1, vector<int>(y + 1, 0)); vector<vector<int>> b(y + 1, vector<int>(z + 1, 0)); vector<int> res(x * z); for (int i = 1; i <= x; i++) { for (int j = 1; j <= y; j++) cin >> a[i][j]; } for (int i = 1; i <= y; i++) { for (int j = 1; j <= z; j++) cin >> b[i][j]; } int c = 0; int i = 1; // 最外层外初始化i while (i<=x) { int k = 1; while (k<=z) { int j = 1; int sum = 0; while (j <= y) { sum += a[i][j] * b[j][k]; j++; } res[c++] = sum; k++; } i++; // 最外层内更新i } for (int i = 0; i < res.size(); i++) { if (i%z != 0) cout << ' '; cout << res[i]; if (i% z == z - 1) cout << endl; } } }
def zz(list1, list2): list3 = list(zip(*list2)) re = [] for row in list1: rr = [] re.append(rr) for low in list3: num = sum(map(lambda x:x[0]*x[1], list(zip(row, low)))) rr.append(num) return re while True: try: x = int(input()) y = int(input()) z = int(input()) rlist = [list(map(int, input().strip().split())) for _ in range(x)] llist = [list(map(int, input().strip().split())) for _ in range(y)] relist = zz(rlist, llist) for r in relist: print(*r) except: break
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int x=sc.nextInt(); int y=sc.nextInt(); int z=sc.nextInt(); int[][] arr1=new int[x][y]; int[][] arr2=new int[y][z]; for(int i=0;i<x;i++){ for(int j=0;j<y;j++){ arr1[i][j]=sc.nextInt(); } } for(int i=0;i<y;i++){ for(int j=0;j<z;j++){ arr2[i][j]=sc.nextInt(); } } int[][] res=new int[x][z]; for(int i=0;i<x;i++){ for(int j=0;j<z;j++){ int sum=0; for(int k=0;k<y;k++){ sum+=arr1[i][k]*arr2[k][j]; } res[i][j]=sum; System.out.print(res[i][j]+" "); } System.out.println(); } } } }
#include "string.h" #include <stdio.h> /*输入两个一维数组计算相乘值*/ int jisuan(int *a,int *b,int lon) { int i=0; int j=0; int k=0; for (i=0;i<lon;i++) k=k+a[i]*b[i]; return k; } int main (void) { int A[2]={0}; int B[2]={0}; int one[100][100]; int two[100][100]; while (scanf("%d",&A[0])!=EOF) { scanf("%d%d",&A[1],&B[1]); B[0]=A[1]; int i=0; int j=0; int z=0; int k=0; int pich[100]={0}; int roll[100]={0}; for (i=0;i<A[0];i++) for (j=0;j<A[1];j++) scanf("%d",&one[i][j]); for (i=0;i<B[0];i++) for (j=0;j<B[1];j++) scanf("%d",&two[i][j]); for (i=0;i<A[0];i++) /*遍历第一个矩阵的行*/ { for (z=0;z<A[1];z++) pich[z]=one[i][z]; /*把第一个矩阵某一行单独拎出来*/ for (j=0;j<B[1];j++) /*遍历第二个矩阵的列*/ { k=0; for (z=0;z<B[0];z++) roll[z]=two[z][j]; /*把第二个矩阵某一列单独拎出来*/ k= jisuan(pich, roll, A[1]); /*计算*/ printf("%d ",k); if (j==B[1]-1) printf("\r\n"); } } } }
#include "string.h" #include <stdio.h> int main (void) { int A[2]={0}; int B[2]={0}; int one[100][100]; int two[100][100]; while (scanf("%d",&A[0])!=EOF) { scanf("%d%d",&A[1],&B[1]); B[0]=A[1]; int i=0; int j=0; int z=0; int k=0; for (i=0;i<A[0];i++) for (j=0;j<A[1];j++) scanf("%d",&one[i][j]); for (i=0;i<B[0];i++) for (j=0;j<B[1];j++) scanf("%d",&two[i][j]); for (i=0;i<A[0];i++) { for (j=0;j<B[1];j++) { for (z=0;z<A[1];z++) { k=k+one[i][z]*two[z][j]; if (z==A[1]-1) { printf("%d ",k); k=0; if (j==B[1]-1) printf("\r\n"); } } } } } }
while True: try: a = int(input()) b = int(input()) c = int(input()) matrix1, matrix2 = [], [] for i in range(a): matrix1.append([int(i) for i in input().split()]) for i in range(b): matrix2.append([int(i) for i in input().split()]) output = [[0 for i in range(a)] for j in range(c)] for i in range(a): for j in range(c): for k in range(b): output[i][j] += matrix1[i][k] * matrix2[k][j] for i in output: print(' '.join([str(k) for k in i])) except: break为什么无法通过调试呢
while True: try: x=int(input()) y=int(input()) z=int(input()) s1=[] s2=[] for i in range(x):#s1,s2构造二维数组,接受矩阵 s1.append(list(map(int,input().split()))) #s1.append(x1 for x1 in list(map(int,input().split()))) for j in range(y): s2.append(list(map(int,input().split()))) res=[] for k in range(x): res.append([0]*z)#构造接受数组,即矩阵 for i in range(x): for j in range(z): k=0 while k<y:#k是关键 res[i][j] +=s1[i][k]*s2[k][j]#累加接受结果 k+=1 for g in res: print(' '.join(str(i) for i in g))#输出 except: break
while True: try: x,y,z,A,B=int(input()),int(input()),int(input()),[],[] for i in range(x): A.append(list(map(int, input().split()))) #读入矩阵A for i in range(y): B.append(list(map(int, input().split()))) #读入矩阵B C ,tmp= [],0 for i in range(x): # A行 for j in range(z): #B列 for k in range(y): #A列,B行 tmp += A[i][k]*B[k][j] #循环进行进行矩阵乘法(A行*B列) C.append(tmp) #保存每个元素tmp到矩阵C中 tmp = 0 for i in range(x): print(' '.join(list(map(str,C[i*z:(i+1)*z])))) except: break
#include<iostream> #include<vector> #include<string> using namespace std; int main() { int x, y, z; while(cin >> x >> y >> z) { vector<vector<int>> A(x, vector<int>(y,0)); vector<vector<int>> B(y, vector<int>(z,0)); vector<vector<int>> C(x, vector<int>(z,0)); //矩阵A for(int i = 0; i < x; i++) for(int j = 0; j < y; j++) cin >> A[i][j]; //矩阵B for(int i = 0; i < y; i++) for(int j = 0; j < z; j++) cin >> B[i][j]; //计算结果 for(int i = 0; i < x; i++) for(int j = 0; j < z; j++) for(int m = 0; m < y; m++) C[i][j] += A[i][m]*B[m][j]; //打印输出 for(int i = 0; i < C.size(); i++) { for(int j = 0; j < C[i].size(); j++) cout << C[i][j] << " "; //没考虑每行最后不输出空格,也能过 cout << endl; } } return 0; }
#include <iostream> //明白矩阵乘法是如何乘的 例如a的第一行,分别乘以b的第一列,取和复制给c using namespace std; int main() { int n,m,k; while(cin >> n>>m>>k) { int a[n][m]; int b[m][k]; int c[n][k]; for(int i = 0;i<n;i++) { for(int j= 0 ;j<m;j++) { int tmp; cin>>tmp; a[i][j] = tmp; } } for(int i = 0;i<m;i++) { for(int j= 0 ;j<k;j++) { int tmp; cin>>tmp; b[i][j] = tmp; } } for(int i =0;i<n;i++) { for(int j = 0;j<k;j++) { int tmp = 0; int res = 0; while(tmp <m) { res += a[i][tmp] * b[tmp][j]; tmp++; } c[i][j] = res; cout<<c[i][j]<<" "; } cout<<endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int x,y,z; while(cin>>x>>y>>z){ vector<vector<int> > a1(x,vector<int>(y,0)); vector<vector<int> > a2(y,vector<int>(z,0)); vector<vector<int> > a3(x,vector<int>(z,0)); for(int i=0;i < x;++i) for(int j=0;j < y;++j) cin >> a1[i][j]; for(int i=0;i < y;++i) for(int j=0;j < z;++j) cin >> a2[i][j]; for(int i=0;i < x;++i) for(int j=0;j < z;++j) for(int k=0;k < y;++k) a3[i][j] += a1[i][k]*a2[k][j]; for(int i=0;i < x;++i){ for(int j=0;j < z;++j) cout << a3[i][j] << ' '; cout << endl; } } return 0; }