A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  下面是原题目,别急,我英语不好也看不懂。下面有我理解的题目意思,和相应的代码,感觉所谓考研复试真题还没黑马的测试题难呢,呵呵。
Maximum Subsequence SumGiven 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 -21Sample Output:10 1 4

  1. /*
  2. 给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。
  3. “最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },
  4. 其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和,以及和最大的子列高低位角标

  5. 输入格式:
  6. 输入第1行给出正整数 K (<= 100000);第2行给出K个整数,其间以空格分隔。

  7. 输出格式:
  8. 在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。

  9. 输入样例:
  10. 6
  11. -2 11 -4 13 -5 -2

  12. 输出样例:
  13. 20 1 3


  14. */
  15. import java.util.Scanner;

  16. class  GetMaxSubsequence
  17. {
  18.     public static void main(String[] args)
  19.     {/*
  20.         Scanner scanner =new Scanner(System.in);
  21.         System.out.println("请输入一串整数序列,每个整数以空格隔开\n输入样例:-2 11 -4 13 -5 -2");
  22.         String str=scanner.nextLine();
  23.         String[] sequence=str.split(" ");
  24.         int[] arr=new int[sequence.length];
  25.         for(int i=0;i<arr.length;i++) {
  26.             arr[i]=new Integer(sequence[i]);
  27.             //System.out.println(arr[i]);
  28.         }*/

  29.         int[] temp={-2,-12,-4,-5,-2,-5,-50,-20,-10,-8,-6};
  30.         System.out.println(getMaxSubsequenceSum(temp));

  31.     }


  32.     public static int getMaxSubsequenceSum(int[] arr){
  33.         int ThisSum=0,MaxSum=0,left=0,right=-1,realleft=-1;
  34.         for(int i=0;i<arr.length;i++){
  35.             ThisSum+=arr[i];
  36.             if(ThisSum>MaxSum){
  37.                 MaxSum=ThisSum;
  38.                 if(i>=left){
  39.                     right=i;
  40.                     realleft=left;
  41.                 }
  42.             }
  43.             else if(ThisSum<0){
  44.                 ThisSum=0;
  45.                 left=i+1;
  46.             }
  47.         }
  48.         System.out.println("i:"+realleft+",j:"+right);
  49.         return MaxSum;
  50.     }
  51. }
复制代码


3 个回复

倒序浏览
只能说学校都是很看重算法的
回复 使用道具 举报
考研注重的扎实的基础,做理论比较多,不属于技术工,人家是搞科研的,方向都不同
回复 使用道具 举报
高大上……
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马