8-1 最大连续和
#include <bits/stdc++.h> using namespace std; const int maxn = 10010; int a[maxn], s[maxn]; int main() { int n; int mins = 999999, ans = -999999; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; s[i] = s[i - 1] + a[i]; mins = min(mins, s[i]); ans = max(ans, s[i] - mins); } cout << ans; }
Make your program run faster depending on its math feature.