在Eclipse中实现数组中最大子数组之和
1.首先代码部分:
public class test {
public int maxSum(int [] sum){
int nSum=sum[0];
int mSum=sum[0];
for(int i=1;i<sum.length;i++){
nSum+=sum[0];
if(nSum<sum[i]){
nSum=sum[i];
}
if(mSum<nSum){
mSum=nSum;
}
}
return mSum;
}
}
测试代码以及运行结果:
import static org.junit.Assert.*;
import org.junit.Test;
public class testTest {
@Test
public void testMaxSum() {
int sum1[]={1,2,2,-1,3};
int sum2[]={1,2,3,-1,3};
test te=new test();
assertTrue(te,mSum(sum1)==7);
assertTrue(te,mSum(sum1)==8);
}
private void assertTrue(test te, boolean b) {
// TODO Auto-generated method stub
}
private int mSum(int[] sum1) {
// TODO Auto-generated method stub
return 0;
}
}