码迷,mamicode.com
首页 > 编程语言 > 详细

求数组最大子数组和

时间:2017-03-17 20:58:27      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:rgs   ++   print   img   test   []   org   created   junit   

package yunsuan;

public class Array {
public static void main(String[] args) {

int[] a = { 1, -2, 3, 10, -4, 7, 2, -5 };
int max = MaxSum(a);
System.out.println(max);
}

public static int MaxSum(int[] a) {
int out = 0;
int temp = 0;
for (int i = 0; i < a.length; i++) {
temp = temp + a[i];
if (temp > out)
out = temp;
if (temp < 0)
temp= 0;
}
return out;
}

}

 

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Created by Tony on 2017/3/17.
 */
public class ArrayTest {
  private Array array;
    int sum []={1, -2, 3, 10, -4, 7, 2, -5};
    @Before
    public void setUp() throws Exception {
      array =new Array();
    }

    @Test
    public void testMaxSum() throws Exception {
        assertEquals(18d,array.maxSum(sum),0);
    }
}

技术分享

 

求数组最大子数组和

标签:rgs   ++   print   img   test   []   org   created   junit   

原文地址:http://www.cnblogs.com/zb1007/p/6568101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!