返回一个二维数组中最大子数组的和
#include<iostream>
using namespace std;
void main ()
{
int x,y,i,j,m=0,A[100][100];
cout<<"输入矩阵的行()和列";
cin>>x>>y;
if(x>100||y>100)
{
cout<<"请重新输入:";
cin>>x>>y;
}
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>A[i][j];
}
}
int sum[100]={0},max=0,result=A[0][0];
for(i=0;i<x;i++)//确定子数组的最大上界(为第i行)
{
while(m+i<x)//确定子数组有m+i行
{
//把子数组当成一位数组一样,求最大子数组的和
for(j=0;j<y;j++)
{
sum[j]=sum[j]+A[m+i][j];
}
max=0;
for(j=0;j<y;j++)
{
if(max+sum[j]>sum[j])
{
max=max+sum[j];
}
else
{
max=sum[j];
}
if(max>result)
{
result=max;
}
}
m++;//是子数组的行数+1
}
//初始化m和sum[]的值,使子数组最大上界下降1,之后重新循环。
m=0;
for(j=0;j<y;j++)
{
sum[j]=0;
}
}
cout<<result;
}