zoukankan      html  css  js  c++  java
  • [PAT] 1007 Maximum Subsequence Sum (25 分)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 1ijK. 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


     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 
     5 /**
     6  * @Auther: Xingzheng Wang
     7  * @Date: 2019/2/18 15:56
     8  * @Description: pattest
     9  * @Version: 1.0
    10  */
    11 public class PAT1007 {
    12     public static void main(String[] args) throws IOException {
    13         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    14         reader.readLine();
    15         String[] split = reader.readLine().split(" ");
    16         int final_max_sum = Integer.MIN_VALUE;
    17         int sub_sum = 0, start_index = 0, final_start = 0, final_end = split.length - 1;
    18         for (int i = 0; i < split.length; i++) {
    19             int value = Integer.valueOf(split[i]);
    20             sub_sum = sub_sum + value;
    21             if (sub_sum >= 0) {
    22                 if (sub_sum > final_max_sum) {
    23                     final_max_sum = sub_sum;
    24                     if (start_index != final_start) {
    25                         final_start = start_index;
    26                     }
    27                     final_end = i;
    28                 }
    29             } else {
    30                 sub_sum = 0;
    31                 start_index = i + 1;
    32             }
    33         }
    34         if (final_max_sum < 0) {
    35             final_max_sum = 0;
    36         }
    37         System.out.print(final_max_sum + " ");
    38         System.out.print(Integer.valueOf(split[final_start]) + " ");
    39         System.out.println(Integer.valueOf(split[final_end]));
    40     }
    41 }
  • 相关阅读:
    推荐一个css抖动库
    八佰
    如何将猫猫监控放在博客上
    一个springboot注解不成功的小问题
    vue整理
    springboot整理
    国外服务器:org.xml.sax.SAXParseException
    记一次tomcat运行起来了但是项目没起来的问题
    一个狗血的mysql编码错误
    js中for(var key in o ){};用法小记
  • 原文地址:https://www.cnblogs.com/PureJava/p/10497952.html
Copyright © 2011-2022 走看看