标签:star space 最大字串 auto 最大字串和 art class amount cas
#include <iostream> #include <string> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define SWAP(a,b) auto c=a;a=b;b=c; int main() { int n, num_amount, num, max, start, end, temp_start, accu; cin >> n; FOR(i, 1, n+1){ cin >> num_amount; max = -1001; temp_start = accu = 0; FOR(temp_end, 0, num_amount){ cin >> num; accu += num; if (accu > max){ start = temp_start; end = temp_end; max = accu; } if(accu < 0){ accu = 0; temp_start = temp_end + 1; } } cout << "Case " << i << ":" << endl; cout << max << " " << start + 1 << " " << end + 1 << endl; if(i != n) cout << endl; } return 0; }
经典的最大字串和问题,没什么好说的,当尝试中的字串和小于 0 时就放弃这个字串,改为尝试以下一个标号开始的字串。
标签:star space 最大字串 auto 最大字串和 art class amount cas
原文地址:https://www.cnblogs.com/KakagouLT/p/13049057.html