跳到正文

个人记录

第11次练习课:T1 小田的九宫格1

原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

题意分析: 现在给定一个n行m列的二维矩阵a,问其中每个3×3的矩阵中,元素总和最大是多少?

思路: 输入完之后,枚举矩阵的左上角,用两个for循环,将以i,j为左上角的3*3子矩阵所有坐标的值加起来,最后找出最大值即可

代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define qwq ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int n,m,a[1005][1005],maxx=INT_MIN;
signed main(){
	qwq;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=n-2;i++){
		for(int j=1;j<=m-2;j++){
			int s=a[i][j]+a[i][j+1]+a[i][j+2]+a[i+1][j]+a[i+1][j+1]+a[i+1][j+2]+a[i+2][j]+a[i+2][j+1]+a[i+2][j+2];
			maxx=max(maxx,s);
		}
	}
	cout<<maxx;
	return 0;
}

错因: 代码错误

原文