题解 | #最大子矩阵#

最大子矩阵

https://www.nowcoder.com/practice/a5a0b05f0505406ca837a3a76a5419b3

#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        int array[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                cin >> array[i][j];
            }
        int max = array[0][0], all = 0;
        for (int j = 0; j < n; j++)
            for (int r = 0; r < n - j; r++) {
                all = 0;
                for (int i = 0; i < n; i++) {
                    for (int p = j; p < j + r + 1; p++) {
                        all += array[p][i];
                    }
                    if (max < all)
                        max = all;
                    if (all < 0)
                        all = 0;
                }
            }
        cout << max << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务