二维前缀和
坑点:因为value为负数,所以max1一定要小,不能简单的让max1初值0。
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; int N, M, C; int sum = 0; int a[maxn][maxn]; int main() // 二维前缀和 { cin >> N >> M >> C; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { int value; cin >> value; a[i][j] = a[i - 1][j] + a[i][j - 1] + value - a[i - 1][j - 1]; } } int xid = 0, yid = 0, max1; for (int i = C; i <= N; i++) { for (int j = C; j <= M; j++) { int q = a[i][j] - a[i - C][j] - a[i][j - C] + a[i - C][j - C]; if (i == C && j == C) { // max1赋初值 max1 = q; xid = i; yid = j; } if (q > max1) { max1 = q; xid = i; yid = j; } } } cout << xid - C + 1 << " " << yid - C + 1; }