Max Sum

来源:互联网 发布:caffe axis 编辑:程序博客网 时间:2024/05/16 07:31

Max Sum

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


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
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
 

Sample Output
Case 1:14 1 4Case 2:7 1 6
 
思路  : 用for循环累加  如果和小于0 就清零 然后再改变first  last   
#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{ int a[100050],i,j,sum1,temp,first,last,t,n,max1;
   scanf("%d",&t);
  for(j=1;j<=t;j++)
   {
       scanf("%d",&n);
       for(i=0;i<n;i++)
        scanf("%d",&a[i]);
        sum1=0,temp=0,last=0,max1=-1001;
       for(i=0;i<n;i++)
       {
           sum1+=a[i];
           if(sum1>max1)
           {
               max1=sum1;
               first=temp;
               last=i;
           }
           if(sum1<0)
           {
               sum1=0;
               temp=i+1;
           }
       }
       printf("Case %d:\n%d %d %d\n",j,max1,first+1,last+1);
       if(j!=t)
        printf("\n");
   }
    return 0;
}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 夏天龟背竹蔫了怎么办 春羽叶子长黄斑怎么办 百合竹叶子发黄掉落怎么办 凤尾蕨叶子蔫了怎么办 绿地珊瑚蕨干了怎么办 翠云草叶子蔫了怎么办 珊瑚蕨叶子烂了怎么办 黑骨茶叶子黑斑怎么办 外场主持没有人互动怎么办 企业年报填错了怎么办 手机忘了放哪了怎么办 燃气卡车没气了怎么办 老房子拆了重建怎么办 加了差的汽油怎么办 事业单位未满5年怎么办 小学孩子不会制订学习计划怎么办 忘记提交原创怎么办百度熊掌号 计算机一级报名表填错了怎么办 离婚了不给孩子生活费怎么办 点击爱奇艺系统提示停止运行怎么办 电视机图像颜色变了怎么办 10万签约被拒怎么办 钢琴弹奏中的折指怎么办! 吉他琴头旋钮将琴头扭坏了怎么办? 1岁幼儿不爱吃饭怎么办 3岁幼儿不爱吃饭怎么办 4岁幼儿不爱吃饭怎么办 买票买了点映 怎么办 一年级孩子注意力不集中怎么办 孩子上一年级注意力不集中怎么办 普宁二中初一新生住宿怎么办 小学入学普查错过了怎么办 初一的孩子不爱学习怎么办 初一孩子出现厌学情况怎么办 客人要求我们代办事项时怎么办 客人要求我们代办事项应该怎么办 想家了特别想哭怎么办 把友谊看得太重怎么办 高一新生数学差怎么办 如果好朋友觉得你成熟阴暗怎么办 被同学抓住把柄敲诈怎么办