• [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 }
  • 相关阅读:
    继承关系中子类使用@Data注解问题
    Professional, Entreprise, Architect版本的区别
    Delphi中ARC内存管理的方向
    技术的止境(客户价值第一,快速实现第二,边做边学,迅速成为牛人。紧贴客户的需求去做技术,立于不败之地。追求的目标:把一项产品去做好,用产品去养活自己和家人)good
    C++ 函数模板与类模板(使用 Qt 开发编译环境)
    C++进阶之虚函数表
    Net反编译软件
    python虚拟环境--virtualenv和virtualenvwrapper
    Windows同时安装python3和python2
    python的pip源在windows和linux修改
  • 原文地址:https://www.cnblogs.com/PureJava/p/10497952.html
Copyright © 2020-2023  润新知