首页 > 编程语言 > 详细

AS 数组内两个最大的和

时间:2017-03-17 23:41:01      阅读:262      评论:0      收藏:0      [点我收藏+]

public class Arrays {
int MaxSubArray(int[] A, int n)
{
int maxSum =A[0];
int currSum = 0;
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
for (int k = i; k <= j; k++)
{
currSum += A[k];
}
if (currSum > maxSum)
maxSum = currSum;

currSum = 0; //这里要记得清零,否则的话sum最终存放的是所有子数组的和。
}
}
return maxSum;
}
}

 

 

 

public class ArraysTest {

private Arrays Array;

@Before
public void setUp() throws Exception {
Array = new Test2();
}

@Test
public void maxSubArray() throws Exception {
int []array1={-1,2,9,-4};
assertEquals(11d,Array.MaxSubArray(array1,array1.length),4);

int []array2={-1,2,3,3,-4};
assertEquals(8d,Array.MaxSubArray(array2,array2.length),5);

int []array3={-1,20,10,30,-4};
assertEquals(60d,Array.MaxSubArray(array3,array3.length),5);

int []array={-2,-3,-5,-1,-9};
assertEquals(-1d,Array.MaxSubArray(array,array.length),5);
}
}

AS 数组内两个最大的和

原文:http://www.cnblogs.com/xuzhinan/p/6569025.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!