#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