求助,不知道哪里错了

P1719 最大加权矩形

tiying @ 2023-08-31 21:53:38

#include<stdio.h>
int m[130][130],n,c,t,max = -200;
int main(){
    scanf("%d",&n);
    for(int i = 1;i<=n;++i){
        for(int j = 1;j<=n;++j){
            scanf("%d",&c);
            m[i][j] = m[i-1][j]+m[i][j-1]-m[i-1][j-1]+c;
        }
    }
    for(int x1 = 1;x1<=n;x1++){
        for(int y1 = 1;y1<=n;y1++){
            for(int x2 = x1;x2<=n;x2++){
                for(int y2 = y1;y2<=n;y2++){
                    c = m[x2][y2]-m[x1-1][y2]-m[x2][y1-1]+m[x1-1][y2-1];
                    if(c>max) max = c;
                }
            }
        }
    }
    printf("%d",max);
    return 0;
}

by 07kzs @ 2023-08-31 22:01:52

@tiying 第15行算式的最后一项,你把 y1 打成 y2 了。

AC code:

#include<stdio.h>
int m[130][130],n,c,t,max = -200;
int main(){
    scanf("%d",&n);
    for(int i = 1;i<=n;++i){
        for(int j = 1;j<=n;++j){
            scanf("%d",&c);
            m[i][j] = m[i-1][j]+m[i][j-1]-m[i-1][j-1]+c;
        }
    }
    for(int x1 = 1;x1<=n;x1++){
        for(int y1 = 1;y1<=n;y1++){
            for(int x2 = x1;x2<=n;x2++){
                for(int y2 = y1;y2<=n;y2++){
                    c = m[x2][y2]-m[x1-1][y2]-m[x2][y1-1]+m[x1-1][y1-1];
                    if(c>max) max = c;
                }
            }
        }
    }
    printf("%d",max);
    return 0;
}

by tiying @ 2023-08-31 22:05:58

@07kzs ......太粗心了,下次得仔细点,谢谢了


|