本文主要是介绍一组整数中求出最大整数序列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【题目来源】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;
}
这篇关于一组整数中求出最大整数序列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!