杭电1003
#include <iostream> using namespace std; int main() { int n, m, i, j; int a[100001]; cin>>n; int line = 1; while (line <= n) { cin>>m; for (i = 0; i < m; i++) { cin>>a[i]; } int max = a[0], sum = 0, start = 0, end = 0, lstart = 0; //下标时从0开始算的,记录第一个值为最大值 for(j = 0; j < m; j++) { sum += a[j]; if (sum > max) //当和大于之前的值时,更新起点和终点 { max = sum; start = lstart; end = j; } if (sum < 0) //当和小于0时,标记下个起点,因为负数加一个数的结果肯定比那个数小,确定不是最优解,状态更新,从下一个点重新记录 { sum = 0; lstart = j+1; } } cout<<"Case "<<line<<":"<<endl; cout<<max<<" "<<start+1<<" "<<end+1<<endl; if (line < n) cout<<endl; line++; } return 0; }