解题思路:
注意事项:
数组开够,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 人评分
WU-格式化数据输出 (C语言代码)浏览:1755 |
WU-C语言程序设计教程(第三版)课后习题11.11 (C++代码)(想学链表的可以看看)浏览:1358 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:550 |
【亲和数】 (C语言代码)浏览:600 |
1126题解浏览:596 |
简单的a+b (C语言代码)浏览:582 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:564 |
蚂蚁感冒 (C语言代码)浏览:775 |
C二级辅导-分段函数 (C语言代码)浏览:601 |
整除问题 (C语言代码)浏览:526 |