A typical divide-and-conquer method that splits large-scale problems and then gradually merges them to get answers
package 算法;
import java.util.Scanner;
public class 最大子序和 {
public static void main(String[] args) {
int[] arr = new int[6];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 6; i++) {
arr[i] = in.nextInt();
}
max_List(arr);
}
static void max_List(int[] a){
System.out.println(max(a,0,a.length-1));
}
static int max(int[] a,int first,int end){
if(first == end){
if(first >= 0)
return a[first];
else
return 0;
}
int temp = (first+end)/2;
int left_list = max(a,first,temp);
int right_list = max(a,temp+1,end);
int s1 = 0,left = 0;
for (int i = temp; i >= first; i--) {
left = left+a[i];
if(left > s1)s1 = left;
}
int s2 = 0,right = 0;
for (int i = temp+1; i < end+1; i++) {
right = right+a[i];
if(right > s2)s2 = right;
}
if((s1 + s2) < left_list && left_list > right_list)
return left_list;
if((s1 + s2) < right_list)
return right_list;
else
return s1+s2;
}
}