杭电 hdu 1003

Max Sum:

本题同样没有AC,解题思路很简单。

#include<iostream>
#include<string.h>
using namespace std;
#define N 100010
int a[N];
int main(){
    int n;
    cin>>n;
    int count=1;
    while(n--){
        int i;
        int m;
        cin>>m;
        for(i=1;i<=m;++i)
            cin>>a[i];
           
        int maxsum=a[1],begin=1,end=1;
           
        int temp=0;
        int k=0;
        for(i=1;i<=m;++i){
            temp+=a[i];
               
            if(temp<0){    //如果和小于0,则舍弃前面的
                temp=0;
                k=i+1;
            }
               
            if(temp>maxsum){
                maxsum=temp;
                begin=k;
                end=i;
            }
        }
           
        if(maxsum==0){
            maxsum=a[1];
            for(i=2;i<=m;++i){
                if(a[i]>maxsum){
                    maxsum=a[i];
                    begin=end=i;
                }
            }
        }
           
        cout<<"Case "<<count<<":"<<endl;
        cout<<maxsum<<" "<<begin<<" "<<end<<endl;
           
        if(n!=0)
            cout<<endl;
           
        memset(a,0,N*sizeof(int));
    }
    return 0;
}


你可能感兴趣的:(ACM,HDU,杭电,1003)