设序列n,起始值为0
状态:s[i]是否加上dp[i - 1]
1.若前一项为正,则dp[i] = dp[i - 1] + s[i]
2.若前一项为非正,则dp[i] = s[i]
3.用max记录最大的dp[i]
#include <stdio.h> #include <string.h> int s[6] = { 0, 6, -1, 5, 4, -7 }; int dp[100]; int main(int argc, char *argv[]) { int sum = 0, max = 0; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= 5; i++) { if (dp[i - 1] > 0) dp[i] = dp[i - 1] + s[i]; else dp[i] = s[i]; if (dp[i] > max) max = dp[i]; } printf("%d\n", max); return 0; }
原文:http://www.cnblogs.com/Cxx-Code/p/3632082.html