• 简单dp hdu1003


    Max Sum

    Problem 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
     
    这题蛮悲剧的 总共交了13次 可能理解的还是不够深吧 有几个地方一直没注意
    View Code
     1 #include<stdio.h>
     2 int a[100001];
     3 int main()
     4 {
     5     long t,n,nowmax,premax,x,y,k = 0,i,start;
     6     scanf("%ld", &t);
     7     while(t--)
     8     {
     9         k++;
    10         scanf("%ld", &n);
    11         for(i = 1 ; i <= n ; i++)
    12             scanf("%ld", &a[i]);          
    13         premax = -999;//因为输入数据大于-1000,premax比-1000大就行了
    14         nowmax = 0;
    15         start = 1;
    16         x = 1;
    17         y = 1;
    18         for(i = 1 ; i <= n ; i++)
    19         {
    20             nowmax+=a[i];
    21             if(nowmax>premax)
    22             {
    23                 y = i;
    24                 start = x;//之前没有定义start 想着用下面的x就可以找出初始位置了 如果之前已经找到最大的premax 而后面又有负的nowmax 那x就不为初始位置 所以这里要跟着变动
    25                 premax = nowmax;
    26             }           
    27             if(nowmax<0)//这里本来写的else if 如果nowmax是负的并且比premax大 就不对了
    28             {
    29                 x = i+1;
    30                 nowmax = 0;
    31             }
    32         }
    33         printf("Case %ld:\n",k);
    34         printf("%ld %ld %d\n", premax,start,y);
    35         if(t!=0)
    36             printf("\n");
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    python 之 re模块、hashlib模块
    python 之 random 模块、 shutil 模块、shelve模块、 xml模块
    python 之 time模块、datetime模块(打印进度条)
    python 之 包的使用
    python 之 序列化与反序列化、os模块
    SAP GUI 750 安装包 及 补丁3 共享
    实例:关于ALV控件可编辑的整理
    SAP 文本框实例
    SAP 日志管理
    TO_DATS() AS ABAP_DATE
  • 原文地址:https://www.cnblogs.com/shangyu/p/2446302.html
Copyright © 2020-2023  润新知