zoukankan      html  css  js  c++  java
  • hdu1003

    Max Sum

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 95590    Accepted Submission(s): 22086

    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
     
     
     
     
     
     
     
    1. #include<iostream>
    2. #include<cstdio>
    3. using namespace std;
    4. int main()
    5. {  
    6. int i,j=0,t;
    7.  scanf("%d",&t);
    8.  while(t--)
    9.  {   
    10. int  n,sum=0,max=-9999999,start,end=0,k=0,;
    11.   scanf("%d",&n);   
    12. for(i=0;i<n;i++)  
    13.  {   
    14.  int a; 
    15.  scanf("%d",&a);  
    16.   sum+=a;  
    17.   if(sum>max)  
    18.   {     
    19. max=sum;    
    20.  start=k+1;  
    21.    end=i+1;   
    22.  }    
    23. if(sum<0)   
    24.  {    
    25.  sum=0;    
    26.  k=i+1;   
    27.  }  
    28.  }        
    29.   cout<<"Case "<<++j<<":"<<endl;    
    30.            cout<<max<<" "<<start<<" "<<end<<endl;   
    31.    if(t>0)     
    32.   cout<<endl;
    33.  }  
    34. return 0;
    35. }
     
  • 相关阅读:
    Nginx是如何配置为 Web 服务器的【转载】
    BeanUtil工具类的使用
    数据库:XML,解析Dom4J
    jsp简单标签开发
    Java 反射 使用总结
    JAVA 对象序列化——Serializable
    使用Session防止表单重复提交
    EasyUi模糊匹配搜索框combobox
    Cookie工作原理
    COOKIE和SESSION有什么区别?
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/2940470.html
Copyright © 2011-2022 走看看