[英]Finding the max integer in an array, with specified start and ending indices in Java, **recursively**
對於我正在做的事情,我需要一個 integer 數組,並從用戶那里獲得兩個索引。 一個是數組中的起點,另一個是終點。 有了這兩點,我必須遞歸地找到數組中的最大值。 我目前擁有的代碼可以工作,但我覺得有些東西可以讓它更流暢,甚至我可以做一些不同的事情來擺脫一些無用的代碼。 我將在下面發布我的代碼。
import java.util.Scanner;
public class RecursiveProblem {
public static void main(String args[]) {
int start, end, size, max;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers would you like to enter in total?");
size = scan.nextInt();
int[] myArray = new int[size];
System.out.println("Please enter the specified starting index.");
start = scan.nextInt() - 1;
System.out.println("Please enter the specified ending index.");
end = scan.nextInt() - 1;
System.out.println("Please enter " + size + " integers seperated by a space, or press "
+ "enter after each number: ");
for(int i = 0; i < myArray.length; i++) {
myArray[i] = scan.nextInt();
}
scan.close();
max = myArray[start];
max = findMax(myArray, start, end, max);
System.out.println("The max of your array between the indices of " + (start + 1) +
"-" + (end + 1) + " is..." + max);
}
public static int findMax(int[] myArray, int start, int end, int max) {
if(start < end) {
if(myArray[start + 1] > max) {
start++;
max = myArray[start];
return findMax(myArray, start, end, max);
}
else {
start++;
return findMax(myArray, start, end, max);
}
}
return max;
}
}
我主要困惑的一件事,但不是我唯一的問題 - 是每當我選擇將 start++ 命令放入 function 調用 findMax(myArray, start++, end, max) <--(像這樣)時,它最終會無限遞歸。 我對為什么這會導致無限遞歸感到困惑,但我這樣做的方式卻沒有。 提前感謝您在清理代碼方面的幫助,並幫助我解決我的問題!
您的代碼總體上沒問題,但是如果您想要簡潔的東西,可以使用下面的代碼:
//Before calling this particular method, you'll have to set the max to
//Integer.MINIMUM_VALUE so it's guaranteed to be changed later OR call it
//with start + 1 instead of start
public static int findMax(int[] myArray, int start, int end, int max) {
return (start < end) ?
findMax(myArray,
start + 1,
end,
(myArray[start] > max) ? myArray[start] : max))
: max;
}
您無限重復的原因是因為后增量運算符的工作方式。
當你說start++
時,它將 start 的值壓入堆棧,然后在后台遞增它。 所以它作為一個獨立的語句很好,但是當你把它寫成findMax(myArray, start++, end, max)
時,它實際上和調用findMax(myArray, start, end, max)
是一樣的,因為參數 start 真的沒有改變。
您走在正確的軌道上,但您的解決方案可以簡化。
關鍵觀察是數組的最大元素是:
第一個元素后數組的rest的最大值
// requires start <= end (not checked, left as exercise for reader) public static int findMax(int[] myArray, int start, int end) { if (start < end) { int maxOfRest = findMax(myArray, start+1, end); return myArray[start] > maxOfRest? myArray[start]: maxOfRest; } else { // start == end return myArray[start]; } }
請注意,我的解決方案不需要將“到目前為止的最大值”帶入 findMax 的每個遞歸激活中——我認為這使它更清晰。
腳注: ?:
運算符正確地稱為條件運算符。 它有時被稱為“三元運算符”,但這意味着“具有三個操作數的運算符”,與各種二元運算符(兩個操作數)和一元運算符(一個操作數)不同,因此它不是特別有用。
您需要傳遞的只是array
和值0
m
初始化為最終最小值。max
直到i == the array length - 1
m
的值從調用堆棧返回時與i
指向的數組中的值進行比較int [] a = {1,3,20,2,55,32,4,9,44,10,29};
int m = max(a,0);
System.out.println(m);
public int max(int[] a, int i) {
int m = Integer.MIN_VALUE;
if (i < a.length-1) {
m = max(a,i+1);
}
return m > a[i] ? m : a[i];
}
印刷
55
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.