package com.refe.algorithm;
import org.junit.Test;
public class MaxSubSum {
int[] array = { 1, -2, 3, 10, -4, 7, 2, -5 };
@Test
public void test() {
maxsum();
}
private void maxsum() {
int tmp = array[0];
int max = tmp;
int low = 0, high = 0;
for (int i = 1; i < array.length; i++) {
if (tmp < 0) {
tmp = array[i];
low = i;
} else
tmp += array[i];
if (tmp >= max) {
max = tmp;
high = i;
}
}
System.out.println(max);
printsub(low, high);
}
private void printsub(int low, int high) {
for (int i = low; i <= high; i++)
System.out.println(array[i]);
}
}
求最大和子序列
最新推荐文章于 2024-07-24 20:44:05 发布