标签:
1.矩阵乘法
输入两个矩阵,分别是m*s,s*n大小。输出两个矩阵相乘的结果。
#include <stdio.h>
int main(int argc, const char * argv[]) {
//输入m,s,n,确定矩形的规模
int m,s,n;
scanf("%d%d%d",&m,&s,&n);
//输入数据,确定两个矩阵
int i,j;
int first[m][s];
int second[s][n];
for (i = 0; i < m; i ++) {
for (j = 0; j < s ; ++j) {
scanf("%d",&first[i][j]);
}
}
for (i = 0; i < s; ++ i) {
for (j = 0; j < n; ++ j) {
scanf("%d",&second[i][j]);
}
}
//两个矩阵相乘
int third[m][n];
int z;
int total=0;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
for (total= z = 0; z < s; ++z) {
total += first[i][z]*second[z][j];
}
third[i][j] = total;
}
}
//将矩阵相乘的结果打印出来
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf("%d ",third[i][j]);
}
printf("\n");
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/123qw/p/4381163.html