zoukankan      html  css  js  c++  java
  • 5thweek.problem_A hdu1003最大子段和

    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:
    ",k);
    46         printf("%d %d %d
    ",maxn,begin,end);
    47         if(t)
    48             printf("
    ");
    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:
    ",k);
    47         printf("%d %d %d
    ",maxn,begin,end);
    48         if(t)
    49             printf("
    ");
    50 
    51     }
    52 
    53     return 0;
    54 }
     
     
  • 相关阅读:
    XV6文件系统
    XV6锁
    PC硬件以及引导加载器
    XV6第一个进程
    XV6操作系统接口
    XV6环境搭建及注意事项
    XV6文件系统
    XV6上下文切换
    少走弯路的十条忠告
    LIBCMTD.lib与libcpmtd冲突的解决方法。
  • 原文地址:https://www.cnblogs.com/x512149882/p/4733124.html
Copyright © 2011-2022 走看看