解题思路:
注意事项:
数组开够,200×200足矣
参考代码:
#include <iostream> #include <stdio.h> #define N 200 using namespace std; typedef struct Mat { int row = 0, col = 0; long long a[N][N]; void clearA(){ for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) a[i][j] = 0LL; } Mat(){ clearA(); //必须,不能省略,否则优化后会造成一些奇怪的后果 } Mat(int row, int col){ clearA(); //必须,不能省略,否则优化后会造成一些奇怪的后果 this->row = row; this->col = col; } void show(){ for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) printf("%ld ", a[i][j]); printf("\n"); } } } Mat; Mat operator * (const Mat a, const Mat b) { Mat ans(a.row, b.col); for (int i = 0; i < a.row; i++) { for (int j = 0; j < b.col; j++) { int x = 0; for (int k = 0; k < a.col; k++) x += a.a[i][k] * b.a[k][j]; ans.a[i][j] = x; } } return ans; } int main() { int aRow = 0, aCol = 0; cin >> aRow >> aCol; Mat a(aRow, aCol); for (int i = 0; i < aRow; i++) for (int j = 0; j < aCol; j++) cin >> a.a[i][j]; int bRow = 0, bCol = 0; cin >> bRow >> bCol; Mat b(bRow, bCol); for (int i = 0; i < bRow; i++) for (int j = 0; j < bCol; j++) cin >> b.a[i][j]; Mat ans = a*b; ans.show(); return 0; }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.9 (Java代码)浏览:1335 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:584 |
WU-整除问题 (C++代码)浏览:612 |
校门外的树 (C语言代码)浏览:702 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:461 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:902 |
1126题解浏览:594 |
1128题解(返回值为数组的情况)浏览:473 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:689 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:1479 |