国产探花免费观看_亚洲丰满少妇自慰呻吟_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); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒙自县| 湘乡市| 和林格尔县| 海城市| 儋州市| 灵寿县| 铁岭市| 诏安县| 安化县| 灵宝市| 苍梧县| 海阳市| 庄河市| 南投市| 鹿邑县| 张家界市| 扶风县| 福清市| 姚安县| 彩票| 海阳市| 舞阳县| 清丰县| 邹城市| 灵武市| 望江县| 马山县| 渝北区| 华池县| 南投市| 望城县| 建平县| 贡嘎县| 兴化市| 独山县| 杨浦区| 黄陵县| 上林县| 土默特右旗| 仁怀市| 盐源县|