#include<stdio.h>
#define MAXLENGTH 1000
int main()
{ int a[MAXLENGTH];
printf("please input the length of the array:\n");
unsigned int n;
int z;
scanf("%d",&n);
int i;
printf("please input the array!\n");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
for (i=0;i<n;i++)
{
printf("%d",a[i]);
}
z=max_subsequence_sum(a,n);
printf("the maximum subsequence sum is %d\n",z );
}
int max_subsequence_sum(int a[], unsigned int n)
{
int this_sum, max_sum,best_i,best_j,i,j,k;
max_sum=0;best_i=best_j= -1;
for (i=0;i<n;i++)//locate begin cursor
{
for(j=i;j<n;j++)//locate end cursor
{
this_sum=0;
for(k=i;k=j;k++)//sum form i to j
this_sum +=a[k];
if(this_sum>max_sum)
{
//update max_sum,best_i,best_j
best_i=i;
best_j=j;
}
}
}
return (max_sum);
}
慕的地6264312
相关分类