这个就很简单了 直接一个循环生成一个新数组 int[] b = {1+4+3,4+3+6,...} 然后求最大数
public static void main(String[] args) { int a[] = {1,4,3,6,3,2,7,8,4,9,3,5,2}; int maxSum = 0; int maxIndex = 0; for(int i=0;i<a.length-3;i++){ int temp = a[i]+a[i+1]+a[i+2]; if(maxSum < temp){ maxSum = temp; maxIndex = i; } } System.out.println(maxSum); System.out.println(maxIndex); }
int max=0; for (int i=0; i<a.length-2; i++) { if (i==0 || (a[i] + a[i+1] + a[i+2]) > max) max = a[i] + a[i+1] + a[i+2]; }
4楼代码有两个小Bug i<a.length-3 数组中如果全是负数,结果会不对
sorry,应该是a.length-2,而不是a.length-3向5楼那样才对
直接上code了public class Test { static int a[] = {1,4,3,6,3,2,7,8,4,9,3,5,2}; static int N = 3;//连续N项和 public static void main(String[] args) { System.out.println("连续" + N + "项和最大值为" + findMax(a, N)); }
/**count 为连续n项和*/ static int findMax(int[] num,int count){ if(num.length < count || count <= 0) return Integer.MAX_VALUE * (-1); //数组长度过小,或者项数为非正 int sum = 0,max = Integer.MAX_VALUE * (-1); for(int i = 0;i < count - 1;i++) sum += num[i]; for(int i = count - 1;i < num.length;i++){ sum += num[i]; if(sum > max) max = sum; sum -= num[i - count + 1]; } return max; } }结果: 连续3项和最大值为21
为什么是3N N吧?13L 滑动是对的
应该只用一次循环。我的代码如下:package com.walkman.suanfa; /** * 作者: 严振 * 日期: 2011-7-11 * 内容: 求连续三个值相加的最大值 * 设计思想:用选择法,先取前三个数的和为最大值,循环进行比较。时间复杂度O(n)。 */ public class MaxSum { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = {1,4,3,6,3,2,7,8,4,9,3,5,2}; int first_max = a[0] + a[1] + a[2]; //确定第一个为比较的值 int comp = 0; for(int i = 1; i < a.length - 3; i++ ){ comp = a[i] + a[i+1] + a[i+2]; if(first_max < comp){ first_max = comp; } } System.out.println("最大的和是: "+first_max); }}
直接一个循环生成一个新数组
int[] b = {1+4+3,4+3+6,...}
然后求最大数
public static void main(String[] args) {
int a[] = {1,4,3,6,3,2,7,8,4,9,3,5,2};
int maxSum = 0;
int maxIndex = 0;
for(int i=0;i<a.length-3;i++){
int temp = a[i]+a[i+1]+a[i+2];
if(maxSum < temp){
maxSum = temp;
maxIndex = i;
}
}
System.out.println(maxSum);
System.out.println(maxIndex);
}
int max=0;
for (int i=0; i<a.length-2; i++) {
if (i==0 || (a[i] + a[i+1] + a[i+2]) > max)
max = a[i] + a[i+1] + a[i+2];
}
i<a.length-3
数组中如果全是负数,结果会不对
sorry,应该是a.length-2,而不是a.length-3向5楼那样才对
static int a[] = {1,4,3,6,3,2,7,8,4,9,3,5,2};
static int N = 3;//连续N项和
public static void main(String[] args) {
System.out.println("连续" + N + "项和最大值为" + findMax(a, N));
}
/**count 为连续n项和*/
static int findMax(int[] num,int count){
if(num.length < count || count <= 0)
return Integer.MAX_VALUE * (-1); //数组长度过小,或者项数为非正
int sum = 0,max = Integer.MAX_VALUE * (-1);
for(int i = 0;i < count - 1;i++)
sum += num[i];
for(int i = count - 1;i < num.length;i++){
sum += num[i];
if(sum > max)
max = sum;
sum -= num[i - count + 1];
}
return max;
}
}结果:
连续3项和最大值为21
为什么是3N N吧?13L 滑动是对的
/**
* 作者: 严振
* 日期: 2011-7-11
* 内容: 求连续三个值相加的最大值
* 设计思想:用选择法,先取前三个数的和为最大值,循环进行比较。时间复杂度O(n)。
*/
public class MaxSum { public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {1,4,3,6,3,2,7,8,4,9,3,5,2};
int first_max = a[0] + a[1] + a[2]; //确定第一个为比较的值
int comp = 0;
for(int i = 1; i < a.length - 3; i++ ){
comp = a[i] + a[i+1] + a[i+2];
if(first_max < comp){
first_max = comp;
}
}
System.out.println("最大的和是: "+first_max);
}}