#include<iostream>
using namespace std;
#include<ctime>
#include<fstream>
void main()
{
int m,n,i,j,smark,mmark,t2;
int sum;
int up[100],down[100],t[100];
int a[100][100],b[100];
cout<<"输入二维数组的行"<<endl;
cin>>m;
cout<<"输入二维数组的列"<<endl;
cin>>n;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
//输入二维数组
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[j]=a[i][j];
}
int c[100]={0};
int sum1=0,max1=0,k;
for(k=0;k<n;k++) //在列上求每一个最大子数组
{
if(sum1<0)
{
sum1=b[k];
}
else
{
sum1=sum1+b[k];
}
c[k]=sum1;
}
max1=c[0];
for(k=0;k<n;k++)
{
if (max1<c[k])
{
max1= c[k];
mmark = k;
}
}
for (k = mmark;k >= 0;k--)
{
if (c[k] == b[k])
{
smark = k;
break;
}
}
sum=max1;
up[i]=smark;
down[i]=mmark;
t[i]=sum;
}
t2=t[0];
for(i=0;i+1<m;i++)
{
if(up[i]<=down[i+1] && down[i]>=up[i+1])
{
t2+=t[i+1];
}
for(j=up[i];j<up[i+1];j++)
{
if(a[i+1][j]>0) t2+=a[i+1][j]; //判别独立正数
}
}
//文件输出
ofstream fout("D:\\\\input.txt",ios::binary);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
fout<<a[i][j]<<" ";
}
fout<<endl;
}
fout<<"最大联通子数组的和为:"<<t2<<endl;
}
同伴的链接:http://www.cnblogs.com/tyyhph/p/5352373.html