• hdu1003 Max Sum (求连续的和最大的自链



    Max Sum

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


    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

    题意:t组数据,长度为n的字符串,找出一段连续且和最大的字符串

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int inf=0x3f3f3f3f;
    int main()
    {
        int t;
        scanf("%d",&t);
        int k=t;
        while(t--)
        {
            int n;
            scanf("%d",&n);
            int maxx=-inf,sum=0;
            int x=1,y=0,temp=1;//temp用来标记起点
            for(int i=0; i<n; i++)
            {
                int a;
                scanf("%d",&a);
                sum+=a;
                if(sum>maxx)//两个if判断不能换位置,例5 -1 -2 -3 -4 -5
                    //这样会提前改变sum和temp的值,从而错误
                {
                    maxx=sum;
                    x=temp;
                    y=i+1;
                }
                if(sum<0)
                {
                    sum=0;
                    temp=i+2;//如果不用temp标记起点,直接用x的话例:5 -1 -2 -3 -4 -5
                    //x会一直加下去,输出就会出错
                    //x=i+1;
                }
    
            }
            printf("Case %d:
    %d %d %d
    ",k-t,maxx,x,y);
            if(t!=0)
                printf("
    ");
        }
        return 0;
    }
    
    这个是错误的代码,我考虑一下我错哪了
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int inf=0x3f3f3f3f;
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            int maxx=-inf,sum=0;
            int x=0,y=0,temp=1;
            //这种代码,有特别多的弊端,
            //值全为0时,maxx,x,y都不对
            //还有只要sum>0都能进入那个循环,y就会一直加,也是错的
            for(int i=0;i<n;i++)
            {
                int a;
                scanf("%d",&a);
                sum+=a;
                if(sum<0)
                {
                    x=i+1;
                    y=i+1;
                    sum=0;
                }
                else
                {
                    maxx=max(maxx,sum);
                    y+=1;
                }
            }
        }
    }


  • 相关阅读:
    python 读写excel,基于win32com实现
    nginx中lua语言获取传参字符串转json打印key,value
    nginx中lua语言打印匹配正则表达式的内容
    nginx 对于post,get参数访问做xss,sql注入过滤
    javaWeb第五天---MVC设计模式
    javaWeb第四天-----jspELJSTL
    javaWeb第三天---http协议和会话技术
    javaWeb第一天
    JDBC第二天---JDBC工具类
    jdbc第三天---配置文件、dao模式
  • 原文地址:https://www.cnblogs.com/zxy160/p/7215114.html
Copyright © 2020-2023  润新知