PAT:最大子列求和问题

最近学MOOC的数据结构课程的一道题目,以下是题目和解答

 

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

题目的大概意思翻译过来是这样的,第一行输入数据个数,第二行输入数据,用空格分开,输出的是最大子序列,第一个数字是最大子序列的和,第二个数字是最大子序列的开头数字,第三个数字是最大子序列的结束数字,然后再加上各种balabal的限制。。。。。i+j最小什么的

----------------------------------------------------------------------------------------------------

 
01  import java.io.BufferedReader;
02  import java.io.IOException;
03  import java.io.InputStreamReader;
04 
05 
06  public class Test {
07 
08  public static void main( String [] args) {
09    // TODO Auto-generated method stub
10    int num;
11    int [] temp;
12    String numStr = null;
13    String numListStr = null;
14   
15    BufferedReader strin = new BufferedReader( new InputStreamReader( System . in));  
16    try {
17    numStr = strin . readLine(); //读取键盘输入,输入为数据长度
18    } catch ( IOException e) {
19    // TODO Auto-generated catch block
20    e . printStackTrace();
21    }  
22    try {
23      numListStr = strin . readLine(); //读取键盘输入,输入为数据,用空格分开
24    } catch ( IOException e) {
25      // TODO Auto-generated catch block
26    e . printStackTrace();
27    }
28    num = Integer . parseInt( numStr); //转换成int
29    temp = new int [ num ]; //根据输入的数据长度创建数组
30    String [] numListArr = numListStr . split( " "); //用空格分割,返回字符串数组
31    for( int i = 0; i
32    temp [ i ] = Integer . parseInt( numListArr [ i ]);
33    }
34   
35    int [] result = findMaxTeam( num , temp); //working。。。
36    System . out . println( result [ 0 ] + " " + result [ 1 ] + " " + result [ 2 ]);
37  }
38 
39  public static int [] findMaxTeam( int n , int [] intArray ){
40    int maxSum = - 1; //最大和,这里设置为-1而不是0,是考虑到测试点出现全负的情况
41    int firstNum_old = intArray [ 0 ]; //第一个数字
42    int firstNum_new = intArray [ 0 ]; //实时更新的第一个数字
43    int lastNum = intArray [ intArray . length - 1 ]; //最后一个数字
44    int thisSum = 0;
45    int [] result = new int [ 3 ];
46    for( int i = 0; i
47    thisSum += intArray [ i ];
48    if ( thisSum > maxSum) {
49      maxSum = thisSum;
50      lastNum = intArray [ i ];
51      firstNum_old = firstNum_new;
52      //flag = true;
53    } else if( thisSum <</SPAN> 0){
54     thisSum = 0;
55     if(i <</SPAN> n-1)firstNum_new = intArray[i+1];
56    }
57   }
58   
59   if (maxSum == -1) {
60    maxSum = 0;
61   }
62   
63   result[0] = maxSum;
64   result[1] = firstNum_old;
65   result[2] = lastNum;
66   
67   return result;
68  }
69 
70 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值