最大子数组 hdu 1003 1231

来源:互联网 发布:c语言中布尔型数据 编辑:程序博客网 时间:2024/05/29 06:27

Max Sum

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


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
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
 

Sample Output
Case 1:14 1 4Case 2:7 1 6
 

Author
Ignatius.L
 

Recommend

We have carefully selected several similar problems for you:  1087 1069 2084 1058 1159 


ac代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){    int n,maxn,p,q,m,k;    cin>>n;    int o=n;    int y=0;    while(n--)    {        y++;        maxn=-100000000;        scanf("%d",&m);        int t=1;        int sum=0;        for(int i=1;i<=m;i++)        {           scanf("%d",&k);           if(sum<0)           {               sum=k;               t=i;           }           else           sum+=k;           if(sum>maxn)           {               maxn=sum;               p=t;               q=i;           }           if(sum<0)           {               sum=k;               t=i;           }        }        cout<<"Case "<<y<<":"<<endl;        cout<<maxn<<" "<<p<<" "<<q<<endl;        if(y!=o)        cout<<endl;    }    return 0;}

最大连续子序列

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32386    Accepted Submission(s): 14513
Problem Description
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个, 例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和 为20。 在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 子序列的第一个和最后一个元素。
 
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
 
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
 
Sample Input
6-2 11 -4 13 -5 -210-10 1 2 3 4 -5 -23 3 7 -2165 -8 3 2 5 01103-1 -5 -23-1 0 -20
 

Sample Output
20 11 1310 1 410 3 510 10 100 -1 -20 0 0
Hint
Hint
Huge input, scanf is recommended.
 

Source
浙大计算机研究生复试上机考试-2005年 

ac代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){    int n,maxn,p,q,m,k;    while(cin>>n&&n)    {        maxn=-100000000;        int t;        int sum=0;        int w=0;        int first,last;        for(int i=1;i<=n;i++)        {           scanf("%d",&k);           if(k<0)           w++;           if(i==1)           {t=k;first=k;}           if(i==n)           last=k;           if(sum<0)           {               sum=k;               t=k;           }           else           sum+=k;           if(sum>maxn)           {               maxn=sum;               p=t;               q=k;           }           if(sum<0)           {               sum=k;               t=k;           }        }        if(w!=n)        cout<<maxn<<" "<<p<<" "<<q<<endl;        else        cout<<0<<" "<<first<<" "<<last<<endl;    }    return 0;}


原创粉丝点击