题解 | #求连续子数组的最大和# 前缀和
求连续子数组的最大和
https://www.nowcoder.com/practice/8705437354604a7cb9ba7bf959e42de6
#include <iostream> #include <vector> using namespace std; vector<int> a; int main() { int x = 0; char c; int p = 1; while((c=getchar()) != '\n'){ if(c == '-'){ p = -1; }else if(c == ','){ a.push_back(x*p); p = 1; x = 0; }else{ x = x*10+(c-'0'); } } a.push_back(x*p); a.insert(a.begin(), 0); int n = a.size(); vector<int> pre(n+1,0); for(int i = 1; i <= n; i++){ pre[i] = pre[i-1]+a[i]; } int res = 0; for(int i = 1; i <= n; i++){ for(int j = i; j <= n; j++){ res = max(res ,pre[j]-pre[i-1] > 0 ? pre[j]-pre[i-1] : 0); } } cout << res << endl; return 0; } // 64 位输出请用 printf("%lld")#每日一题#