首页 > 代码库 > 最大子序列和

最大子序列和

动态规划的算法:

#include<stdio.h>int MaxSubsequenceSum(const int A[],int n){    int i,sum,MaxSum;    sum=MaxSum=0;    for(i=0;i<n;i++)    {        sum+=A[i];        if(sum>MaxSum)            MaxSum=sum;        if(sum<0)            sum=0;    }    return MaxSum;}void main(){    int arr[10]={3,4,-2,5,-4,6,-2,8,-9,-23};    int max=MaxSubsequenceSum(arr,10);    printf("%d\n",max);}