PTA上做一道题,一个测试点怎么也过不去,求解答

匿名网友 |浏览1070次
收藏|2020/04/30 13:04

满意回答

2020/04/30 13:13

#include <stdio.h>#include <stdlib.h>int main() { int a[10000]; int k, i, left, isright, isleft, NowSum = 0, MaxSum = -10000; scanf("%d", &k); for (i = 0; i < k; i++) { scanf("%d", &a[i]); }//用在线处理算法 for (i = 0; i < k; i++) { NowSum += a[i]; if (NowSum < 0) { NowSum = 0; left = i + 1; //临时的序列最左边项 } else if (NowSum > MaxSum) { MaxSum = NowSum; isright = i; //序列最右边,如果最新的NowSum大于原来的MaxSum,就改变isright的值 isleft = left; //最终的序列最左边 } } if (MaxSum < 0) printf("0 %d %d\n", a[0], a[k - 1]); //全部序列为0时 else printf("%d %d %d\n", MaxSum, a[isleft], a[isright]); return 0;}注:原文链接:https://blog.csdn.net/qq_45900103/java/article/details/104762162希望能帮到你,满意请采纳!谢谢!

daydaup

其他回答(0)
0人关注该问题
+1

 加载中...