zoukankan      html  css  js  c++  java
  • 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

    Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

    Input Specification:

    Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

    Output Specification:

    For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

    Sample Input:

    10
    -10 1 2 3 4 -5 -23 3 7 -21
    

    Sample Output:

    10 1 4
    题目大意:求最大连续子序列和,输出最大的和以及这个子序列的开始值和结束值。
    如果所有数都小于0,那么认为最大的和为0,并且输出首尾元素。
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 int num[10001];
     5 int main()
     6 {
     7     int n,flag=0; 
     8     int i;
     9     int sum=0,msum=-1,first=0,last=0,tempfirst=0;  //系列和,最大子系列和,首位,末位,暂时首位
    10     scanf("%d",&n);
    11     for( i=0; i<n; i++)
    12     {
    13         scanf("%d",&num[i]);
    14         if( num[i]>=0)  //flag处理全为负数情况
    15             flag=1;
    16     }
    17     if( !flag) //全为负数和为1
    18         printf("0 %d %d",num[0],num[n-1]);
    19     else
    20     {
    21         for( i=0; i<n; i++)
    22         {
    23             sum += num[i];
    24             if( sum>msum)
    25             {
    26                 msum = sum;
    27                 first = tempfirst;
    28                 last = i;
    29             }
    30             else if( sum<0 )
    31             {
    32                 sum=0;
    33                 tempfirst = i+1;
    34             }
    35         }
    36         printf("%d %d %d",msum,num[first],num[last]);
    37     }
    38     return 0;
    39 }
    
    
    在这个国度中,必须不停地奔跑,才能使你保持在原地。如果想要寻求突破,就要以两倍现在速度奔跑!
  • 相关阅读:
    Canvas 五角星绘制
    Bob Waters
    自定义样式,使用浏览器阅读epub格式的电子书
    FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)
    【转载】什么样的妻子什么样的人生
    win10与Ubantu双系统:Linux下开启FTP服务器与创建无线热点(实现文件共享)
    这世上,没有完美的父母,也没有完美的孩子
    考研数学复习建议与资料
    eclipse中修改jsp文件中内容,但是刷新网页内容却不变问题
    JavaScript基础2
  • 原文地址:https://www.cnblogs.com/yuxiaoba/p/8543179.html
Copyright © 2011-2022 走看看