public class MaxSum {
/**
* @param array
* @return
*/
static int zx=1;
public static int maxSubSumQuadratic(Integer[] array) {
int maxSum = 0;
for(int start = 0; start < array.length; start++) {
int thisSum = 0;
for(int end = start; end < array.length; end++) {
thisSum += array[end];
if(thisSum > maxSum) {
zx=zx+1; System.out.println(zx);
zx=zx+1;
maxSum = thisSum;
}
}
}
return maxSum;
}
没有主程序,没有测试数据,没法帮你看
你程序中唯一的输出语句是System.out.println(zx);
如果没有输出,那么就是if(thisSum > maxSum)没有执行,或者end < array.length不满足,一直没有循环起来
thisSum += array[end];
修改为
thisSum += array[end].intValue();
如果还不行,那么要看你的数组array中有没有负数,长度是不是0
你调用这个方放的时候有没有用变量接受返回值 我这边试的没有问题
需要在程序末端加入打印语句
public class Test
{
public static int maxSequence(int[] a)
{
// Global max value
int maxsum;
// Current max value
int maxhere;
maxsum = a[0];
maxhere = a[0];
for (int i = 1; i < a.length; i++)
{
if (maxhere <= 0)
{
// Reset current max valule with a[i]
maxhere = a[i];
}
else
{
// Add a[i] to current max value
maxhere += a[i];
}
if (maxhere > maxsum)
{
// Update global max value
maxsum = maxhere;
}
}
return maxsum;
}
public static void main(String[] args)
{
int[] array = {-2, 11, 8, -4, -1, 16, 5, 0};
System.out.println(maxSequence(array));
}
}
代码找问题最快的方式就是debug,现在你在这里问一个序列求和下次你可能会问一个递归,找个vc类的编译器断点调试一下,调试改正自己的变成思路。