1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <iostream> using namespace std; int main() { int dimension,matrix[100][100]; while(cin >> dimension) { for(int i=0;i<dimension;i++) for(int j=0;j<dimension;j++) cin >> matrix[i][j]; /*for(int i=0;i<dimension;i++) for(int j=0;j<dimension;j++) cout << matrix[i][j];*/ int max=matrix[0][0]; for(int i=0;i<dimension;i++) for(int j=0;j<dimension;j++) { int sum=matrix[i][j]; //cout << sum << " "; if(sum<0) { //cout << endl; continue; } for(int k=j;k<dimension;k++) { for(int l=i;l<dimension;l++) { if(k==j&&l==i) continue; sum+=matrix[l][k]; //cout << sum << " "; if(sum<0) break; if(sum>max) max=sum; } } //cout << endl; } if(max<0) { int temp=matrix[0][0]; for(int i=0;i<dimension;i++) for(int j=0;j<dimension;j++) if(matrix[i][j]>temp) temp=matrix[i][j]; } else cout << max << endl; } } |
Direct link: https://paste.plurk.com/show/355773