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 }
  • 相关阅读:
    mac中导出CSV格式在excel中乱码
    Android Gradle与Gradle插件的对应关系
    【算法】二叉树的前序、中序、后序、层序遍历和还原。
    关于Java虚拟机
    从Java synchronized和volatile说起
    【程小白】Java基本特性
    Android一键锁屏APP
    Fragment的生命周期
    学习大数据必须了解的大数据开发课程大纲
    学习大数据这三个关键技术是一定要掌握!
  • 原文地址:https://www.cnblogs.com/PureJava/p/10497952.html
Copyright © 2011-2022 走看看