国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

1003.Max Sum

2019-11-06 07:19:01
字體:
來源:轉載
供稿:網友
PRoblem DescriptionGiven a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.InputThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).OutputFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.Sample Input25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5Sample OutputCase 1:14 1 4Case 2:7 1 6

求每一位置的最大連續和,取最大的就好了

#include<iostream>using namespace std;int main(){ int N; cin >> N; for (int t = 1;t <= N;t++) { if (t != 1) putchar('/n'); printf("Case %d:/n", t); int M; cin >> M; int s_min=0, s_v=1; int s_max=-1001, s_h,s_l; int sum = 0; for (int i = 1;i <= M;i++) { int te;cin >> te; sum += te; if (sum - s_min >= s_max) { s_max = sum - s_min;s_h = i;s_l = s_v; } if (sum < s_min) { s_min = sum;s_v = i + 1; } } printf("%d %d %d/n", s_max, s_l, s_h); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中牟县| 高碑店市| 华蓥市| 林甸县| 册亨县| 隆林| 郎溪县| 晋中市| 呈贡县| 宜阳县| 临夏县| 扎鲁特旗| 元江| 廊坊市| 屏边| 滦南县| 锡林郭勒盟| 三都| 鱼台县| 和政县| 绵竹市| 大新县| 安义县| 大邑县| 阳江市| 澄迈县| 苏尼特右旗| 华容县| 大城县| 兖州市| 浙江省| 揭阳市| 延川县| 昌江| 尖扎县| 会宁县| 新泰市| 南溪县| 沙河市| 霍林郭勒市| 乐都县|