`
scholers
  • 浏览: 615169 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

一道JAVA面试题

阅读更多

   题目:一列整型数组,从第一个开始,累加后续两个数字,得到一个新的数组,然后判断新的数组中的最大值,从而得到这个最大值是原始数组中哪三个数字累加的,输出(打印)这三个数字,并且输出(打印)出这个最大值。

   如:2,5,-6,12,7,15,6,-10

  结果应该是输出:12,7,15

   最大值: 34

 

 

 我的思路1:

  1.先统计处累加的数组,数组长度是原始数组长度-2

  2.计算新数组的最大值

  3.根据最大值的位置输出原始数组的三个数字

  4。输出最大值

 

 思路2(如果可以打乱原始数组的次序):

  1。先冒泡排序一次,从小到大排序

  2。计算最后三个数字的值,得到结果

 

 

 

 

分享到:
评论
15 楼 zlx19900228 2010-10-19  
得到了新数组的最大值,那新数组的当前元素的下标,下标+1,下标+2就是原数组相加的3个元素
14 楼 zhuandiqiu 2010-10-19  
每次遍历一个数,判断这个数是不是比当前和最大的三个数最小的数还要小,如果小就不用加了,在一些情况下可以快一点
13 楼 lyw985 2010-10-19  
dsjt 写道
lyw985 写道
感觉这题变简单了,
以前的题目是一列整型数组,找出最大的连续和值和起始下标
例子:4,-7,6,-10,-3,3,2 答案为:6,下标是2(以0开始)




import java.util.Hashtable;
import java.util.Map;

public class Test {

	public static void main(String[] args) {
		System.out.println(calcMaxSum(new int[] { 9, -11, 2, 3, 4, -10, 6, 3 }));
		System.out.println(calcMaxSum(new int[] { 2, -3, 1, 10, -6, -3 }));
		System.out.println(calcMaxSum(new int[] { 4, -3, 1, 10, -6, -3 }));
		System.out.println(calcMaxSum(new int[] { 3, -3, 1, 10, -6, -3 }));
		System.out.println(calcMaxSum(new int[] { 2, -3, 1, 10, -6, 7 }));
	}

	/**
	 * 返回值的 key 数组index ,value 为连续的数量(包括自身)
	 * 
	 * @param ns
	 * @return
	 */
	public static Map<Integer, Integer> calcMaxSum(int[] ns) {
		Map<Integer, Integer> map = new Hashtable<Integer, Integer>();

		// 最大连续和值(未考虑 int 型求和溢出问题)
		int maxSum = ns[ns.length - 1];
		// 向索引增大方向所能取得的最大连续和值
		int seriesSum = 0;
		// 向索引增大方向所能取得最大连续和值时,的最大索引
		int endIndex = ns.length - 1;

		for (int i = ns.length - 1; i >= 0; i--) {

			if (seriesSum > 0) {
				seriesSum += ns[i];
			} else {
				seriesSum = ns[i];
				endIndex = i;
			}

			if (seriesSum > maxSum) {
				maxSum = seriesSum;
				map.clear();
				map.put(i, endIndex - i + 1);
			} else if (seriesSum == maxSum) {
				map.put(i, endIndex - i + 1);
			}
		}
		return map;

	}

	

}



挺好~
12 楼 jwx0925 2010-10-19  
scholers 写道
jwx0925 写道
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 2, 5, -6, 12, 7, 15, 6, -10 };
		Test.find(arr);
	}

	public static void find(int[] arr) {
		int maxIndex = 0;
		int maxValue = arr[0] + arr[1] + arr[2];
		for (int i = 1; i < arr.length - 2; i++) {
			int temp = arr[i] + arr[i + 1] + arr[i + 2];
			if (temp > maxValue) {
				maxIndex = i;
				maxValue = temp;
			}
		}
		System.out.println("数组:" + arr[maxIndex] + "," + arr[maxIndex + 1]
				+ "," + arr[maxIndex + 2]);
		System.out.println("最大值:" + maxValue);
	}

}




这里果然人才济济啊,很多人都回答的很正确;
这个方法最简单了。


其实有个问题没考虑到,就是加入还有另一个数组,3个数加起来也等于34呢?结果就是这样了

数组:(12,7,15)  (13,5,14)
最大值:34

这样程序就复杂了点。。。
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 2, 5, -6, 12, 7, 15, 6, -10, 38 };
		Test.find(arr);
	}

	public static void find(int[] arr) {
		int[] maxIndex = new int[arr.length - 3];
		int index = 0;
		int maxValue = arr[0] + arr[1] + arr[2];
		for (int i = 1; i < arr.length - 2; i++) {
			int temp = arr[i] + arr[i + 1] + arr[i + 2];

			if (temp == maxValue) {
				index++;
				maxIndex[index] = i;
			}
			if (temp > maxValue) {
				maxValue = temp;
				maxIndex[0] = i;
				for (int j = 1; j < maxIndex.length; j++) {
					maxIndex[j] = 0;
				}
			}
		}
		for (int i = 0; i < maxIndex.length; i++) {
			if (i == 0)
				System.out.println("数组:" + arr[maxIndex[i]] + ","
						+ arr[maxIndex[i] + 1] + "," + arr[maxIndex[i] + 2]);
			if (maxIndex[i] != 0 && i != 0) {
				System.out.println("数组:" + arr[maxIndex[i]] + ","
						+ arr[maxIndex[i] + 1] + "," + arr[maxIndex[i] + 2]);
			}
		}
		System.out.println("最大值:" + maxValue);
	}

}



看看能不能优化~
11 楼 scholers 2010-10-19  
jwx0925 写道
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 2, 5, -6, 12, 7, 15, 6, -10 };
		Test.find(arr);
	}

	public static void find(int[] arr) {
		int maxIndex = 0;
		int maxValue = arr[0] + arr[1] + arr[2];
		for (int i = 1; i < arr.length - 2; i++) {
			int temp = arr[i] + arr[i + 1] + arr[i + 2];
			if (temp > maxValue) {
				maxIndex = i;
				maxValue = temp;
			}
		}
		System.out.println("数组:" + arr[maxIndex] + "," + arr[maxIndex + 1]
				+ "," + arr[maxIndex + 2]);
		System.out.println("最大值:" + maxValue);
	}

}




这里果然人才济济啊,很多人都回答的很正确;
这个方法最简单了。
10 楼 jwx0925 2010-10-19  
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 2, 5, -6, 12, 7, 15, 6, -10 };
		Test.find(arr);
	}

	public static void find(int[] arr) {
		int maxIndex = 0;
		int maxValue = arr[0] + arr[1] + arr[2];
		for (int i = 1; i < arr.length - 2; i++) {
			int temp = arr[i] + arr[i + 1] + arr[i + 2];
			if (temp > maxValue) {
				maxIndex = i;
				maxValue = temp;
			}
		}
		System.out.println("数组:" + arr[maxIndex] + "," + arr[maxIndex + 1]
				+ "," + arr[maxIndex + 2]);
		System.out.println("最大值:" + maxValue);
	}

}

9 楼 dsjt 2010-10-19  
lyw985 写道
感觉这题变简单了,
以前的题目是一列整型数组,找出最大的连续和值和起始下标
例子:4,-7,6,-10,-3,3,2 答案为:6,下标是2(以0开始)




import java.util.Hashtable;
import java.util.Map;

public class Test {

	public static void main(String[] args) {
		System.out.println(calcMaxSum(new int[] { 9, -11, 2, 3, 4, -10, 6, 3 }));
		System.out.println(calcMaxSum(new int[] { 2, -3, 1, 10, -6, -3 }));
		System.out.println(calcMaxSum(new int[] { 4, -3, 1, 10, -6, -3 }));
		System.out.println(calcMaxSum(new int[] { 3, -3, 1, 10, -6, -3 }));
		System.out.println(calcMaxSum(new int[] { 2, -3, 1, 10, -6, 7 }));
	}

	/**
	 * 返回值的 key 数组index ,value 为连续的数量(包括自身)
	 * 
	 * @param ns
	 * @return
	 */
	public static Map<Integer, Integer> calcMaxSum(int[] ns) {
		Map<Integer, Integer> map = new Hashtable<Integer, Integer>();

		// 最大连续和值(未考虑 int 型求和溢出问题)
		int maxSum = ns[ns.length - 1];
		// 向索引增大方向所能取得的最大连续和值
		int seriesSum = 0;
		// 向索引增大方向所能取得最大连续和值时,的最大索引
		int endIndex = ns.length - 1;

		for (int i = ns.length - 1; i >= 0; i--) {

			if (seriesSum > 0) {
				seriesSum += ns[i];
			} else {
				seriesSum = ns[i];
				endIndex = i;
			}

			if (seriesSum > maxSum) {
				maxSum = seriesSum;
				map.clear();
				map.put(i, endIndex - i + 1);
			} else if (seriesSum == maxSum) {
				map.put(i, endIndex - i + 1);
			}
		}
		return map;

	}

	

}
8 楼 xtuul 2010-10-19  
<p>还算比较简单:</p>
<pre name="code" class="java"> public void toPrintMax(int[] oldArray){
String str=oldArray[0]+","+oldArray[1]+","+oldArray[2];
int max=oldArray[0]+oldArray[1]+oldArray[2];
for(int i=1;i&lt;oldArray.length-2;i++){
if(oldArray[i]+oldArray[i+1]+oldArray[i+2]&gt;max){
max=oldArray[i]+oldArray[i+1]+oldArray[i+2];
str=oldArray[i]+","+oldArray[i+1]+","+oldArray[i+2];
}
}
System.out.println(str);
System.out.println(max);
}</pre>
<p>  </p>
7 楼 scholers 2010-10-19  
<div class="quote_title">kimmking 写道</div>
<div class="quote_div">
<div class="quote_title">scholers 写道</div>
<div class="quote_div">
<div class="quote_title">Kisses99 写道</div>
<div class="quote_div">
<div class="quote_title">scholers 写道</div>
<div class="quote_div">
<p>   题目:一列整型数组,从第一个开始,累加后续两个数字,得到一个新的数组,然后判断新的数组中的最大值,从而得到这个最大值是原始数组中哪三个数字累加的,输出(打印)这三个数字,并且输出(打印)出这个最大值。</p>
<p><span style="font-size: 14.4px;">   如:2,5,-6,12,7,15,6,-10</span></p>
<p><span style="font-size: 14.4px;">  结果应该是输出:</span><span style="font-size: 14.4px;">12,7,15</span></p>
<p><span style="font-size: 14.4px;">   最大值: 34</span></p>
<p> </p>
<p> </p>
<p> 我的思路1:</p>
<p>  1.先统计处累加的数组,数组长度是原始数组长度-2</p>
<p>  2.计算新数组的最大值</p>
<p>  3.根据最大值的位置输出原始数组的三个数字</p>
<p>  4。输出最大值</p>
<p> </p>
<p> 思路2(如果可以打乱原始数组的次序):</p>
<p>  1。先冒泡排序一次,从小到大排序</p>
<p>  2。计算最后三个数字的值,得到结果</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p>思路2不符合题意。</p>
</div>
<p> </p>
<p>  后来想了一下,思路2确实不符合题意</p>
</div>
<p><br>一次循环就够了。 </p>
<p>max = 0 </p>
<p>for i = 0-&gt;n-2 </p>
<p>  sum = a(i) + a(i + 1) + a(i + 2) </p>
<p>  if(sum &gt; max) </p>
<p>    index = i </p>
<p>    max = sum </p>
<p>  end if </p>
<p>end for </p>
<p>print a(index),a(index+1),a(index+2) </p>
<p>print max</p>
</div>
<p> </p>
<p>  很简单的解决方法!</p>
6 楼 lyw985 2010-10-19  
感觉这题变简单了,
以前的题目是一列整型数组,找出最大的连续和值和起始下标
例子:4,-7,6,-10,-3,3,2 答案为:6,下标是2(以0开始)
5 楼 kimmking 2010-10-19  
<div class="quote_title">scholers 写道</div>
<div class="quote_div">
<div class="quote_title">Kisses99 写道</div>
<div class="quote_div">
<div class="quote_title">scholers 写道</div>
<div class="quote_div">
<p>   题目:一列整型数组,从第一个开始,累加后续两个数字,得到一个新的数组,然后判断新的数组中的最大值,从而得到这个最大值是原始数组中哪三个数字累加的,输出(打印)这三个数字,并且输出(打印)出这个最大值。</p>
<p><span style="font-size: 14.4px;">   如:2,5,-6,12,7,15,6,-10</span></p>
<p><span style="font-size: 14.4px;">  结果应该是输出:</span><span style="font-size: 14.4px;">12,7,15</span></p>
<p><span style="font-size: 14.4px;">   最大值: 34</span></p>
<p> </p>
<p> </p>
<p> 我的思路1:</p>
<p>  1.先统计处累加的数组,数组长度是原始数组长度-2</p>
<p>  2.计算新数组的最大值</p>
<p>  3.根据最大值的位置输出原始数组的三个数字</p>
<p>  4。输出最大值</p>
<p> </p>
<p> 思路2(如果可以打乱原始数组的次序):</p>
<p>  1。先冒泡排序一次,从小到大排序</p>
<p>  2。计算最后三个数字的值,得到结果</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p>思路2不符合题意。</p>
</div>
<p> </p>
<p>  后来想了一下,思路2确实不符合题意</p>
</div>
<p><br>一次循环就够了。 </p>
<p>max = 0 </p>
<p>for i = 0-&gt;n-2 </p>
<p>  sum = a(i) + a(i + 1) + a(i + 2) </p>
<p>  if(sum &gt; max) </p>
<p>    index = i </p>
<p>    max = sum </p>
<p>  end if </p>
<p>end for </p>
<p>print a(index),a(index+1),a(index+2) </p>
<p>print max</p>
4 楼 scholers 2010-10-19  
<div class="quote_title">Kisses99 写道</div>
<div class="quote_div">
<div class="quote_title">scholers 写道</div>
<div class="quote_div">
<p>   题目:一列整型数组,从第一个开始,累加后续两个数字,得到一个新的数组,然后判断新的数组中的最大值,从而得到这个最大值是原始数组中哪三个数字累加的,输出(打印)这三个数字,并且输出(打印)出这个最大值。</p>
<p><span style="font-size: 14.4px;">   如:2,5,-6,12,7,15,6,-10</span></p>
<p><span style="font-size: 14.4px;">  结果应该是输出:</span><span style="font-size: 14.4px;">12,7,15</span></p>
<p><span style="font-size: 14.4px;">   最大值: 34</span></p>
<p> </p>
<p> </p>
<p> 我的思路1:</p>
<p>  1.先统计处累加的数组,数组长度是原始数组长度-2</p>
<p>  2.计算新数组的最大值</p>
<p>  3.根据最大值的位置输出原始数组的三个数字</p>
<p>  4。输出最大值</p>
<p> </p>
<p> 思路2(如果可以打乱原始数组的次序):</p>
<p>  1。先冒泡排序一次,从小到大排序</p>
<p>  2。计算最后三个数字的值,得到结果</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p>思路2不符合题意。</p>
</div>
<p> </p>
<p>  后来想了一下,思路2确实不符合题意</p>
3 楼 scholers 2010-10-19  
<div class="quote_title">asme2u 写道</div>
<div class="quote_div">
<div class="quote_title">scholers 写道</div>
<div class="quote_div">
<p>   题目:一列整型数组,从第一个开始,累加后续两个数字,得到一个新的数组,然后判断新的数组中的最大值,从而得到这个最大值是原始数组中哪三个数字累加的,输出(打印)这三个数字,并且输出(打印)出这个最大值。</p>
<p><span style="font-size: 14.4px;">   如:2,5,-6,12,7,15,6,-10</span></p>
<p><span style="font-size: 14.4px;">  结果应该是输出:</span><span style="font-size: 14.4px;">12,7,15</span></p>
<p><span style="font-size: 14.4px;">   最大值: 34</span></p>
<p> </p>
<p> </p>
<p> 我的思路1:</p>
<p>  1.先统计处累加的数组,数组长度是原始数组长度-2</p>
<p>  2.计算新数组的最大值</p>
<p>  3.根据最大值的位置输出原始数组的三个数字</p>
<p>  4。输出最大值</p>
<p> </p>
<p> 思路2(如果可以打乱原始数组的次序):</p>
<p>  1。先冒泡排序一次,从小到大排序</p>
<p>  2。计算最后三个数字的值,得到结果</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<br>一次遍历,记下最大的累加值和下标就行了</div>
<p> </p>
<p> </p>
<p>  这位兄弟说的很好,关键值就是最大的累加值和下标了。</p>
2 楼 Kisses99 2010-10-19  
<div class="quote_title">scholers 写道</div>
<div class="quote_div">
<p>   题目:一列整型数组,从第一个开始,累加后续两个数字,得到一个新的数组,然后判断新的数组中的最大值,从而得到这个最大值是原始数组中哪三个数字累加的,输出(打印)这三个数字,并且输出(打印)出这个最大值。</p>
<p><span style="font-size: 14.4px;">   如:2,5,-6,12,7,15,6,-10</span></p>
<p><span style="font-size: 14.4px;">  结果应该是输出:</span><span style="font-size: 14.4px;">12,7,15</span></p>
<p><span style="font-size: 14.4px;">   最大值: 34</span></p>
<p> </p>
<p> </p>
<p> 我的思路1:</p>
<p>  1.先统计处累加的数组,数组长度是原始数组长度-2</p>
<p>  2.计算新数组的最大值</p>
<p>  3.根据最大值的位置输出原始数组的三个数字</p>
<p>  4。输出最大值</p>
<p> </p>
<p> 思路2(如果可以打乱原始数组的次序):</p>
<p>  1。先冒泡排序一次,从小到大排序</p>
<p>  2。计算最后三个数字的值,得到结果</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p>思路2不符合题意。</p>
1 楼 asme2u 2010-10-18  
<div class="quote_title">scholers 写道</div><div class="quote_div"><p>   题目:一列整型数组,从第一个开始,累加后续两个数字,得到一个新的数组,然后判断新的数组中的最大值,从而得到这个最大值是原始数组中哪三个数字累加的,输出(打印)这三个数字,并且输出(打印)出这个最大值。</p>
<p><span style="font-size: 14.4px;">   如:2,5,-6,12,7,15,6,-10</span></p>
<p><span style="font-size: 14.4px;">  结果应该是输出:</span><span style="font-size: 14.4px;">12,7,15</span></p>
<p><span style="font-size: 14.4px;">   最大值: 34</span></p>
<p> </p>
<p> </p>
<p> 我的思路1:</p>
<p>  1.先统计处累加的数组,数组长度是原始数组长度-2</p>
<p>  2.计算新数组的最大值</p>
<p>  3.根据最大值的位置输出原始数组的三个数字</p>
<p>  4。输出最大值</p>
<p> </p>
<p> 思路2(如果可以打乱原始数组的次序):</p>
<p>  1。先冒泡排序一次,从小到大排序</p>
<p>  2。计算最后三个数字的值,得到结果</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p></div><br/>一次遍历,记下最大的累加值和下标就行了

相关推荐

Global site tag (gtag.js) - Google Analytics