码迷,mamicode.com
首页 > 其他好文 > 详细

HDUOJ 1003 Max Sum

时间:2015-10-02 01:28:31      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int c, input, maxSum, frontPos, tempFrontPos, tailPos, tempSum;
 8     int ct = 1;
 9     scanf("%d", &c);
10     while(c--)
11     {
12         tempSum = 0;
13         maxSum = -1000000000;
14         tempFrontPos = frontPos = tailPos = 0;
15         int n;
16         scanf("%d", &n);
17         for(int i = 0; i < n; ++i)
18         {
19             scanf("%d", &input);
20             tempSum += input;
21             if(tempSum > maxSum)
22             {
23                 maxSum = tempSum;
24                 tailPos = i;
25                 frontPos = tempFrontPos;
26             }
27             if(tempSum < 0 && (i != n - 1)) //i != n - 1 保证 frontPos 不会指到一个不存在的位置
28             {
29                 tempSum = 0;
30                 tempFrontPos = i + 1;
31             }
32         }
33         printf("Case %d:\n", ct++);
34         printf("%d %d %d\n", maxSum, frontPos+1, tailPos+1);
35         if(c != 0) printf("\n");
36     }
37     return 0;
38 }

 

HDUOJ 1003 Max Sum

标签:

原文地址:http://www.cnblogs.com/Artprog/p/4851782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!