标签:div int 负数 图片 定义 最大连续子数组 The cti section
public class MaxArray {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int[] array = new int[6];
for(int i=0;i<array.length;i++){
array[i]=scanner.nextInt();
}
int max=maxSumArray(array);
System.out.println(max);
}
public static int maxSumArray(int[] array){
if(array.length==0){
return 0;
}
int sum=0;
int max=0;
for(int i=0;i<array.length;i++){
if(sum<=0){
sum=array[i];
}else{
sum+=array[i];
}
if(sum>max){
max=sum;
}
}
return max;
}
}
条件组合 | 执行路径 |
---|---|
sum<=0,sum>max | acdef |
sum<=0,sum<=max | acdf |
sum>0,sum>max | abdef |
sum>0,sum<=max | abdf |
import static org.junit.Assert.*;
import org.junit.Test;
public class MaxArrayTest {
int[] array={2,4,-6,1};
int[] arraynull={};
@Test
public void testMaxSumArray() {
assertEquals(0, new MaxArray().maxSumArray(arraynull));
assertEquals(6,new MaxArray().maxSumArray(array));
}
}
标签:div int 负数 图片 定义 最大连续子数组 The cti section
原文地址:https://www.cnblogs.com/whjykgz/p/10733947.html