用c++ 编程计算m×s阶矩阵A和s×n阶矩阵B之积
时间: 2024-02-26 15:57:39 浏览: 90
好的,下面是 C++ 代码实现:
```cpp
#include <iostream>
using namespace std;
const int MAXN = 1005; // 矩阵最大行列数
int A[MAXN][MAXN], B[MAXN][MAXN], C[MAXN][MAXN]; // 定义三个矩阵
int main()
{
int m, s, n;
cout << "请输入矩阵 A 的行数和列数:";
cin >> m >> s;
cout << "请输入矩阵 B 的行数和列数:";
cin >> s >> n;
// 输入矩阵 A
cout << "请输入矩阵 A:" << endl;
for (int i = 0; i < m; i++)
for (int j = 0; j < s; j++)
cin >> A[i][j];
// 输入矩阵 B
cout << "请输入矩阵 B:" << endl;
for (int i = 0; i < s; i++)
for (int j = 0; j < n; j++)
cin >> B[i][j];
// 计算矩阵 C = A * B
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
C[i][j] = 0;
for (int k = 0; k < s; k++)
C[i][j] += A[i][k] * B[k][j];
}
// 输出矩阵 C
cout << "矩阵 A * B = " << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cout << C[i][j] << " ";
cout << endl;
}
return 0;
}
```
希望能帮助到你!
阅读全文