zoukankan      html  css  js  c++  java
  • 最大子段和

    Max Sum

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


    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
    //动规思想
    #include<iostream>
    #define MIN -999999
    using namespace std;
    
    int main()
    {
    	int T,i,j,sum,n,cnt,start,end,a[100001],ans;
    
    	cin>>T;
    
    	cnt=0;
    	while(cnt<T)
    	{
    		cin>>n;
    
    		cnt++;
    		ans = MIN;
    		sum=0;
    		j=1;//临时起始下标;
    
    		for(i=1;i<=n;i++)
    		{
    			cin>>a[i];
    			sum+=a[i];
    			if(sum>ans){ans=sum;start=j;end=i;}
    			if(sum<0){sum=0;j=i+1;}
    		}
    		cout<<"Case "<<cnt<<":"<<endl;
    		cout<<ans<<" "<<start<<" "<<end<<endl;
    		if(cnt!=T)cout<<endl;
    	}
    	system("pause");
    	return 0;
    }
    

      

    世上无难事,只要肯登攀。
  • 相关阅读:
    内置函数
    win10 下安装meteror
    每日十问(3)
    白话带你理解什么是编程
    什么是对象的方法
    Python之列表推导式
    英语对学习软件开发重要吗?
    python3中的range函数返回的是列表吗?
    文件读写
    神奇的字符编码
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3427291.html
Copyright © 2011-2022 走看看