首页 > 其他 > 详细

求最大子数组的和

时间:2014-03-19 14:23:53      阅读:438      评论:0      收藏:0      [点我收藏+]

                                                         求最大子数组的和

                                                                                                             张纯,黄思思

 

 

 

#include<iostream.h>
int sum(int *a,int s,int f)
{
if(s>f)
return 0;
int sum=0;
for(int i=s;i<=f;i++)
{
sum=sum+a[i];
}
return sum;
}
int main()
{
int n;
cout<<"please input the number of array:"<<endl;
cin>>n;
int *a=new int[n];
int *b=new int[n];
int *c=new int[n];
int p=0;
cout<<"please input the array:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int k=0;
int l=0;
for(int j=0;j<n;j++)
{
if(j==0&&a[j]>0)
{
b[k]=j;
k++;
}
else if(j!=0&&a[j]>0&&a[j-1]<=0)
{
b[k]=j;
k++;
}
if(j==(n-1)&&a[j]>0)
{
c[l]=j;
l++;
}
else if(j!=(n-1)&&a[j]>0&&a[j+1]<=0)
{
c[l]=j;
l++;
}
}
int max=sum(a,b[0],c[0]),S=b[0],F=c[0];
for(int s=0;s<k;s++)
{
for(int f=0;f<l;f++)
{
if(b[s]<c[f]&&max<=sum(a,b[s],c[f]))
{
S=b[s];
F=c[f];
max=sum(a,b[s],c[f]);
}
}
}
cout<<"the index of front is:"<<S<<endl<<"the index of tail is:"<<F<<endl;
cout<<"the Max of the stuArrray is:"<<max<<endl;
return 0;
}bubuko.com,布布扣

求最大子数组的和,布布扣,bubuko.com

求最大子数组的和

原文:http://www.cnblogs.com/hsslove/p/3611141.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!