一组整数中求出最大整数序列

来源:互联网 发布:中美大单 知乎 编辑:程序博客网 时间:2024/06/05 22:52

【题目来源】hdu 1003

【题目含义】给定一列整形数字,求出最大整数序列的和以及起始位置。

【代码】

#include<iostream>#include<cstdlib>#include<cstdio>#include<memory.h>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<queue>#define mem(array)  memset((array),0,sizeof((array)))#define Qsort(array,len,cmp) qsort(array,len,sizeof(array[0]),cmp)#define inf 0x7fffffff#define MAXN 10+5000using namespace std;int cmpChar(const void *a , const void *b){    return *(char *)a - *(char *)b;  /*** from small to large */    //return *(char *)b - *(char *)a;  /*** from large to small*/}int cmpInt(const void *a , const void *b){    return *(int *)a - *(int *)b;  /*** from small to large */    //return *(int *)b - *(int *)a;  /*** from large to small*/    /***    qsort(num,100,sizeof(num[0]),cmpInt);    */}int cmpInt2(const void *a, const void *b){    //return (*(Node*)a)->x - (*(Node*)b)->x; /*** from small to large */    //return (*(Node*)b)->x - (*(Node*)a)->x;   /*** from large to small*/    return 1;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int T;    cin>>T;    for(int k = 1; k <= T; ++k){        printf("Case %d:\n",k);        int n;        cin>>n;        int sum = 0;        int max = -inf;        int start = 0;        int end  = 0;        int s = 0;        int x;        for(int i = 0; i < n; ++i){            scanf("%d",&x);            sum += x;            if(sum > max){                max = sum;                start = s;                end = i;            }            if(sum < 0){                sum = 0;                s = i+1;            }        }        printf("%d %d %d\n",max,start+1,end+1);        if(k < T)            cout<<endl;    }    return 0;}


原创粉丝点击