解题思路:
注意事项:
数组开够,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语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:2676 |
注意格式(C语言代码)浏览:782 |
A+B for Input-Output Practice (III) (C语言代码)浏览:652 |
C语言训练-排序问题<2> (C++代码)(sort函数)浏览:1720 |
C二级辅导-同因查找 (C语言代码)浏览:626 |
C二级辅导-统计字符 (C语言代码)浏览:528 |
简单的a+b (C语言代码)浏览:674 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:863 |
C语言训练-自由落体问题 (C语言代码)浏览:650 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:537 |