1 回答

TA貢獻1817條經驗 獲得超6個贊
#include<stdio.h>
#define MAXLENGTH 1000
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 k=j改為 k<=j才是i到j的和
this_sum +=a[k];
if(this_sum>max_sum)
{
//update max_sum,best_i,best_j
best_i=i;
best_j=j;
max_sum=this_sum;//更新最大值
}
}
}
return (max_sum);
}
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 );
}
- 1 回答
- 0 關注
- 99 瀏覽
添加回答
舉報