1. 题目
【例题】循环输入,每组数据总共输入两个矩阵,每个矩阵先输入 n 和 m, 然后再输入一个 n x m 的矩阵,输出这两个矩阵的乘积,如果不存在则输出 NULL
2. 解法
#include <iostream>
#include <math.h>
const int MAX = 100;
int input_matrix(int a, int b, int M[MAX][MAX])
{
std::cout << "input matrix value, must " << a << " row, " << b << " column" << std::endl;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
std::cin >> M[i][j];
}
}
return 0;
}
int output_matrix(int a, int b, int M[MAX][MAX])
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
std::cout << M[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
int multi_matrix(int n, int m, int a, int b, int A[MAX][MAX], int B[MAX][MAX], int ret[MAX][MAX])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < b; j++)
{
for (int k = 0; k < m; k++)
{
ret[i][j] += A[i][k] * B[k][j];
}
}
}
output_matrix(n, b, ret);
}
int main()
{
int x;
std::cout << "must input 2" << std::endl;
while (std::cin >> x)
{
int n, m, a, b;
int matA[MAX][MAX];
int matB[MAX][MAX];
int ret[MAX][MAX];
std::cout << "input first matrix row and column, n x m" << std::endl;
std::cin >> n >> m;
input_matrix(n, m, matA);
output_matrix(n, m, matA);
std::cout << "input second matrix, row and column, a x b" << std::endl;
std::cin >> a >> b;
input_matrix(a, b, matB);
output_matrix(a, b, matB);
if (m != a)
{
std::cout << "NULL" << std::endl;
std::cout << "input repeat" << std::endl;
}
else
{
multi_matrix(n, m, a, b, matA, matB, ret);
}
}
return 0;
}
输出结果:
wohu@ubuntu:~/cpp/$ ./main
must input 2
2
input first matrix row and column, n x m
2 3
input matrix value, must 2 row, 3 column
1 2 3
4 5 6
1 2 3
4 5 6
input second matrix, row and column, a x b
3 2
input matrix value, must 3 row, 2 column
7 8
5 4
3 2
7 8
5 4
3 2
26 22
71 64
|