状态转移方程:
#include<stdio.h>
#define max(x,y) (x)>(y)?(x):(y)
#define MAXN 20
#include<iostream>
using namespace std;
int n=6;
int a[]={0,-2,11,-4,13,-5,-2};
int dp[MAXN];
void maxSubSum(){
dp[0]=0;
for(int j=1;j<=n;j++){
dp[j]=max(dp[j-1]+a[j],a[j]);
}
}
void dispmaxSum(){
int maxj=1;
for(int j=2;j<=n;j++){
if(dp[j]>dp[maxj]) maxj=j;
}
int k;
for(k=maxj;k>=1;k--){
if(dp[k]<=0) break;
}
cout<<"最大连续子序列和"<<dp[maxj]<<endl;
cout<<"所选序列:"<<endl;
for(int i=k+1;i<=maxj;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
int main(){
maxSubSum();
dispmaxSum();
}