题目地址:http://www.patest.cn/contests/mooc-ds/01-2
计算最大子列和,并打印出子列的第一个元素和最后一个元素
#include <cstdio> using namespace std; #define MAXN 100000 int main() { int list[MAXN]; int n,i; int ThisSum, MaxSum; int head,tail; scanf("%d",&n); int start = 0,end = n-1; for (int i = 0; i < n; ++i) { scanf("%d",&list[i]); } ThisSum = MaxSum = 0; for (int i = 0; i < n; ++i) { if (ThisSum >=0) { ThisSum += list[i]; tail = i; }else { ThisSum = list[i]; head = i; tail = i; } if (ThisSum > MaxSum ||(ThisSum == 0 && MaxSum == 0)) { MaxSum = ThisSum; start = head; end = tail; } } printf("%d %d %d ",MaxSum, list[start], list[end]); return 0; }