zoukankan      html  css  js  c++  java
  • Max Sum

    Max Sum

     

    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. 
    InputThe 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). 
    OutputFor 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

    其实,我以前做过一次,也是改了半天,迷迷瞪瞪的不知道改了哪里。。。。。。。。这次记下来了。
    题意:求给定的序列的最大的连续的子序列的和和从哪到哪的位置。
    举个例子:6 1 -2 3 4 0 -9 则和最大为3+4+0; 这个0也算。
    而且,如果第一位也是0,那么那个0也算,当如果序列全为负数,则应该输出最大的那个负数和他的位置。
            就拿我写的这个来看,关键在两个if判断,这俩if是同级的,虽然是同级的,但是位置还是不可以替换,
    因为存在全部都为负数的情况,你懂我的意思吧!!!!这里,max,sum,e,f,min(代表中间值)。比较sun+上了一个新的数和max的大小,
    如果大了,就f后移一个,如果加上一个新的数后小与0了。

    for(int i=1;i<=m;i++)
            {
                int node;
                scanf("%d",&node);
                sum+=node;
                if(sum>=max)
                {
                    max=sum;
                    e=min;
                    f=i;
                }
                if(sum<0)
                {
                    sum=0;
                    min=i+1;
                }
    
    

    AC 代码:

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int n;
    int main()
    {
        scanf("%d",&n);
        int k=0;
        for(int j=0;j<n;j++)
        {
            int m;
            scanf("%d",&m);
            int sum=0;
            int max=-1010;
            int e,f;
            int min=1;
            for(int i=1;i<=m;i++)
            {
                int node;
                scanf("%d",&node);
                sum+=node;
                if(sum>=max)
                {
                    max=sum;
                    e=min;
                    f=i;
                }
                if(sum<0)
                {
                    sum=0;
                    min=i+1;
                }
            }
            k++;
            printf("Case %d:
    ",k);
            printf("%d %d %d
    ",max,e,f);
            if(k!=n)
            {
              printf("
    ");
            }
        }
        return 0;
    }

    今天也是元气满满的一天!good luck!




    我想要变得高一点,最好能伸手给你一片天。
  • 相关阅读:
    初级模拟电路:4-9 级联系统
    初级模拟电路:4-8 集电极反馈电路(交流分析)
    初级模拟电路:4-7 共集放大电路(交流分析)
    初级模拟电路:4-6 共射放大电路(交流分析)
    初级模拟电路:4-5 共基放大电路(交流分析)
    初级模拟电路:4-4 re模型详解
    Python语法速查: 15. 解释器与执行环境
    图解Python网络编程
    Python 安装 virturalenv 虚拟环境
    【独孤九剑】运维技术浅谈
  • 原文地址:https://www.cnblogs.com/cattree/p/7445758.html
Copyright © 2011-2022 走看看