问题:
给定一个序列a1,a2..........an;求子序列的和最大问题dp[i]表示以ai结尾的子序列和,max为最大子序列的和。
#include <iostream> using namespace std; #define MAXSIZE 100 int a[MAXSIZE]; int dp[MAXSIZE]; int max = 0; int main() { int n; cin >> n; memset(dp, 0, MAXSIZE); for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { if (dp[i-1] + a[i] > 0) { dp[i] = dp[i - 1] + a[i]; } else { dp[i] = 0; } if (max < dp[i]) max = dp[i]; } cout << max << endl; return 0; }