hdu 1003 Max Sum


下面模拟过程:

1.首先,读取第一个数据,令now和max等于第一个数据,初始化first,last,x位置

2.然后,读入第二个数据,判断 ①. 若是now+next<next,表示当前读入的数据比之前存储的加上当前的还大,说明可以在当前另外开始记录,更新now=next ②. 反之,则表示之前的数据和在增大,更新now=now+next

3.之后,把now跟max做比较,更新或者不更新max的值,记录起始、末了位置

4.循环2~3步骤,直至读取数据完毕。

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,i,m,max,now,j,next,first,last,x;
 6     cin>>n;
 7     for(i=1;i<=n;i++)
 8     {
 9         cin>>m;
10         cin>>next;
11         now=max=next;
12         last=1;first=1;x=1;
13         for(j=2;j<=m;j++)
14         {
15             cin>>next;
16             if(now+next<next)
17             {
18                 now=next;
19                 x=j;
20                 
21             }
22             else
23             {
24                 now+=next;
25             }
26             if(now>max)
27             {
28                 max=now;
29                 first=x;
30                 last=j;
31             }
32         }
33         cout<<"Case "<<i<<":"<<endl;
34         cout<<max<<" "<<first<<" "<<last<<endl;
35         if(i!=n)
36         cout<<endl;
37     }
38 } 

 

posted on 2014-09-10 00:30  薛定谔的熊猫  阅读(130)  评论(0编辑  收藏  举报

导航