| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1605 人关注过本帖
标题:HDU 1003 子序列的最大和
只看楼主 加入收藏
康明贤
Rank: 2
来 自:NWPU
等 级:论坛游民
帖 子:46
专家分:32
注 册:2017-10-23
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:4 
HDU 1003 子序列的最大和
Max Sum
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 293520    Accepted Submission(s): 69675


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

Sample Output
Case 1:
14 1 4

Case 2:
7 1 6

/*#include <stdlib.h>
struct sumtail
{
    int sum;
    int tail;
};

struct sumtail maxSubSum(int a[],int n)
{
    struct sumtail max;
    max.sum=0;
    int sum;
    int i,j,k;
    for(i=0;i<n;i++)
    {
        for(j=i;j<n;j++)
        {
            sum=0;
            for(k=i;k<=j;k++)
            {
                sum+=a[k];
            }
            if(sum>max.sum)
            {
                max.sum  = sum;
                max.tail=j+1;
            }
        }
    }
    return max;
}

void print(int m,int N,struct sumtail max)
{
    if(N>1)
    {
        printf("Case %d:\n",m-N);
        printf("%d 1 %d",max.sum,max.tail);
        printf("\n\n");
    }
    else
    {
        printf("Case %d:\n",m-N);
        printf("%d 1 %d",max.sum,max.tail);
        printf("\n");
    }

}

int main()
{
    int N,n;
    int i;
    struct sumtail maxsum;
    scanf("%d",&N);
    int m=N;
    while(N--)
    {
        int a[1000];
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
           maxsum = maxSubSum(a,n);
        }
        print(m,N,maxsum);
    }
    return 0;
}
*/
#include <stdio.h>
int main()
{
    int N;
    int count=1;
    scanf("%d",&N);
    while(N--){
        int n,i;
        int a[100002];
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int max=a[1];
        int sum=0;
        int l=1,left=1,right=1;
        for(i=1;i<=n;i++){
            sum=sum+a[i];
            if(sum>max){
                max=sum;
                left=l;
                right=i;
            }
            if(sum<0){
                sum=0;
                l=i+1;
            }
        }
        printf("Case %d:\n",count);
        printf("%d %d %d\n",max,left,right);
        if(N)printf("\n");
        count++;
    }
    return 0;
}
第一个程序为什么不能AC?
搜索更多相关主题的帖子: Max Sum the int printf 
2018-08-12 08:37
no1xijin
Rank: 9Rank: 9Rank: 9
来 自:江西
等 级:贵宾
威 望:20
帖 子:211
专家分:1207
注 册:2015-7-8
收藏
得分:10 
AC是什么?

126邮箱联系方式:no1xijin@126. com
2018-08-13 08:52
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:10 

你是不是看到示例中
Case 1:
14 1 4

Case 2:
7 1 6
全是 1,所以
void print(int m,int N,struct sumtail max)
{
    if(N>1)
    {
        printf("Case %d:\n",m-N);
        printf("%d 1 %d",max.sum,max.tail);
        printf("\n\n");
    }
    else
    {
        printf("Case %d:\n",m-N);
        printf("%d 1 %d",max.sum,max.tail);
        printf("\n");
    }
}
2018-08-13 09:02
康明贤
Rank: 2
来 自:NWPU
等 级:论坛游民
帖 子:46
专家分:32
注 册:2017-10-23
收藏
得分:0 
回复 3楼 rjsp


千里之行,始于足下。
2018-08-14 14:36
康明贤
Rank: 2
来 自:NWPU
等 级:论坛游民
帖 子:46
专家分:32
注 册:2017-10-23
收藏
得分:0 
回复 2楼 no1xijin
accepted

千里之行,始于足下。
2018-08-14 14:39
快速回复:HDU 1003 子序列的最大和
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027008 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved