标签:
说实话,这不是自己写的。
十几次debug就忍不住百度了下算法。
和自己开始写的很接近,可我解决不了在不增加时间复杂度的基础上判断负数temp加上下一个数变为正,看了别人的伪码,我想复杂了。
这确实是一道面试题。叹。
#include<iostream>
#include<time.h>
#define random(x) (rand()%x)
using namespace std;
void main()
{
srand((int)time(0));
int a[100];
int i,n;
int temp=0,sum=0;
cout<<"几个数?"<<endl;
cin>>n;
for(i=0;i<n;i++)
a[i]=random(20)-10;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<n;i++)
{
if(temp>0)
{
temp+=a[i];
}
else
{
temp=a[i];
}
if(temp>sum)
sum=temp;
}
cout<<endl<<sum<<endl;
}

标签:
原文地址:http://www.cnblogs.com/apak/p/4354667.html