#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000
int buf [ARRAY_SIZE];
int main()
{
srand((unsigned int )time(0));
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%100-10;
for(i=1;i<=n;i++)printf("%d ",buf[i]);
printf("\n");
//creat random value of buffer and print
//max subsequence sum
long int max_sum=0;
int start=1,end,rs;
for(i=1;i<=n;i++)
{
long int this_sum=0;
start=i;
for(j=i;j<=n;j++)
{
this_sum+=buf[j];
if(this_sum>max_sum)
{
max_sum=this_sum;
rs=start;
end=j;
}
}
}
printf("rs=%d,end=%d,max_sum=%ld\n",rs,end,max_sum);//print result
}
}
//O(n*n)暴力遍历
时间: 2024-10-22 07:19:21