HDU 1003

一道思维题,求和最大的子序列

https://vjudge.net/problem/HDU-1003

#include<iostream>

using namespace std;

int num[100005];

int main(){
    int t,n,i;
    cin>>t;

    for(i = 1;i <=t;i++){
        int start = 1,end,sum = 0,ans = -INT_MAX,temp= 1;    // ans赋值一定要为负
        cin>>n;
        //输入 
        for(int j = 1;j <= n;j++)
            cin>>num[j];
            
        //核心代码 
        for(int k = 1;k <=n;k++){
            if(sum >= 0)
                sum += num[k];
            else{
                sum = num[k];
                temp = k;
            }
            
            if(sum > ans){
                ans = sum;
                start = temp;
                end = k;
            }
        }
        //输出 
        cout<<"Case "<<i<<":\n";
        cout<<ans<<" "<<start<<" "<<end<<endl;
        if(i!=t)
            cout<<endl; 
        
    }
    
    return 0;
} 

 

posted @ 2019-03-28 23:01  LightAc  阅读(118)  评论(0编辑  收藏  举报
返回顶端