zoukankan      html  css  js  c++  java
  • HDU 1003——Max Sum(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

    题目大意:历遍所有数字,找出最大字段和。

    解题思路:

    t和n:记录循环次数和每一段有多少个数字

    temp,now,max:temp存放临时读取的变量,now代表现在和,max代表当前最大和,如果前面相加后是负数,而后一位是正数,则更新起点位置。

    代码如下:

     1 #include <iostream>  
     2 using namespace std;
     3 
     4 int main()  
     5 {  
     6     int t,n,temp,pos1,pos2,max,now,x,i,j;  
     7     scanf("%d",&t);
     8     for (i=1;i<=t;i++)  
     9     {  
    10         scanf("%d%d",&n,&temp);  
    11         now=max=temp;  
    12         pos1=pos2=x=1;  
    13         for (j=2;j<=n;j++)  
    14         {  
    15             scanf("%d",&temp);  
    16             if (now+temp<temp)  //如果前面是负数
    17                 now=temp,x=j;  
    18             else  
    19                 now+=temp;
    20 
    21             if (now>max)  
    22                 max=now,pos1=x,pos2=j;  
    23         }  
    24         printf("Case %d:
    %d %d %d
    ",i,max,pos1,pos2);  
    25         if (i!=t)  
    26             printf("
    ");  
    27     }  
    28     return 0;  
    29 }
  • 相关阅读:
    Robberies
    Big Event in HDU
    UVA 562 Dividing coins
    Little Rooks
    Bone Collector
    Piggy-Bank
    0-1背包问题之——饭卡
    Monkey and Banana
    The Triangle
    Burning Midnight Oil
  • 原文地址:https://www.cnblogs.com/shadervio/p/5758328.html
Copyright © 2011-2022 走看看