题目地址:http://acm.jlu.edu.cn/joj/showproblem.php?pid=1092
代码:
#include <iostream>
using namespace std;
int MaxSum(int n,int a[])
{
int sum = 0;
int b = 0;
for (int i = 1;i <= n;i ++)
{
if (b > 0) b += a[i];
else b =a[i];
if (b > sum) sum = b;
}
return sum;
}
int main()
{
int n;
cin >> n; //n边长
int a[n+1][n+1];
for (int i = 1;i <= n;i ++)
for (int j = 1;j <= n;j ++)
cin >> a[i][j];
int sum = 0;
int *b = new int[n+1];
for (int i = 1;i <= n;i ++)
{
for (int k = 1;k <= n;k ++)
b[k] = a[i][k]; 高手指教啊
for (int j = i + 1;j <= n;j ++)
{
for (int k = 1;k <=n;k ++)
b[k] += a[j][k];
int max = MaxSum(n,b);
if (max > sum)
sum = max;
}
}
cout << sum << endl;
}