子数组的和2

时间:2015-04-01 23:36:35   收藏:0   阅读:327
技术分享技术分享

#include<iostream> using namespace std; #define Nu 100000 int main() { int a[Nu],k; cout<<"随机产生数组为:"; for(int i=0;i<Nu;i++) { k=rand()%2; do a[i]=rand(); while(a[i]>100); a[i]=k==1?a[i]:-a[i]; cout<<a[i]<<" "; } cout<<endl; int sum=a[0],b=a[0],x=0,y=0; for(int i=0; i<Nu; i++) { if(b<0) { b=a[i];x=i;} else b+=a[i]; if(sum<=b) { sum=b;y=i;} } cout<<x<<endl<<y<<endl; cout<<"相邻子数组最大和为:"<<sum<<endl; cout<<"相应子数组为:"; for(int i=x;i<=y;i++) cout<<a[i]<<" "; cout<<endl; return 0; }


 

原文:http://www.cnblogs.com/xiaowumao/p/4385636.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!