沐里纷纷


私信TA

用户名:Epoch

访问量:62719

签 名:

我不会算法

等  级
排  名 37
经  验 12805
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:


注意事项:

数组开够,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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区