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

5thweek.problem_A hdu1003最大子段和

时间:2015-08-15 22:48:01      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

Description

Given 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. 
 

Input

The 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). 
 

Output

For 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 Input

2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
 

Sample Output

Case 1: 14 1 4
Case 2: 7 1 6
 
分析:
DP题。状态转移方程:dp[i]=d[i-1]+a[i]>a[i]?d[i-1]+a[i]:a[i];
 
错误代码如下:(关键错误和正确代码的对比)
技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int m=100005;
 5 int a[m];
 6 
 7 int main()
 8 {
 9     int t,k=0;
10     scanf("%d",&t);
11     while(t--)
12     {
13         int n,i,maxn,before,begin=1,end=1;
14         scanf("%d",&n);
15         for(i=1;i<=n;i++)
16         {
17 
18             scanf("%d",&a[i]);
19             if(i==1)
20              {
21                  maxn=a[i]; before=a[i];
22              }
23              else
24              {
25                 if(before+a[i]<a[i])
26                 {
27                      before=a[i];
28                      begin=i;
29                 }
30                 else
31                 {
32                    before+=a[i];
33                 }
34 
35              }
36              if(before>maxn)
37              {
38                   maxn=before;
39                   //x=begin;
40                   end=i;
41              }
42 
43         }
44         k++;
45         printf("Case %d:\n",k);
46         printf("%d %d %d\n",maxn,begin,end);
47         if(t)
48             printf("\n");
49 
50     }
51 
52     return 0;
53 }
View Code

正确代码如下:(用一个特殊点的案例6      :2 3 -6 4 3 -8找出错误!加入一个关键变量j记录当前a[i]位置)

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int m=100005;
 5 int a[m];
 6 
 7 int main()
 8 {
 9     int t,k=0;
10     scanf("%d",&t);
11     while(t--)
12     {
13         int n,i,maxn,before,begin=1,end=1,j=1;  //必须用一个j记录当前数a[i]的位置
14         scanf("%d",&n);
15         for(i=1;i<=n;i++)
16         {
17 
18             scanf("%d",&a[i]);
19             if(i==1)
20              {
21                  maxn=a[i]; before=a[i];
22 
23              }
24              else
25              {
26                 if(before+a[i]<a[i])
27                 {
28                      before=a[i];
29                      j=i;     //记录当前位置,此时begin不改变
30                 }
31                 else
32                 {
33                    before+=a[i];
34                 }
35 
36              }
37              if(before>maxn)
38              {
39                   maxn=before;
40                     begin=j;  //只有当before>maxn时再改变
41                   end=i;
42              }
43 
44         }
45         k++;
46         printf("Case %d:\n",k);
47         printf("%d %d %d\n",maxn,begin,end);
48         if(t)
49             printf("\n");
50 
51     }
52 
53     return 0;
54 }

 

 
 

5thweek.problem_A hdu1003最大子段和

标签:

原文地址:http://www.cnblogs.com/x512149882/p/4733124.html

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