英文:
Recursion in java (includes array)
问题
public class HelloWorld {
public static int w(int[] a, int start, int k) {
if (start == k) return start;
else {
int m = w(a, start, k - 1);
if (a[m] > a[k]) return m;
else return k;
}
}
public static void main(String[] args) {
int[] arr = {1, 3, 2, 8, 3, 6, 9};
System.out.println(w(arr, 1, 5));
}
}
输出为 3
。谢谢!
英文:
Hey I have a java code and want to know what it does.
public class HelloWorld{
public static int w(int []a, int start, int k)
{
if (start==k) return start;
else
{
int m = w(a, start, k-1);
if(a[m] > a[k]) return m;
else return k;
}
}
public static void main(String []args){
int [] arr = {1,3,2,8,3,6,9};
System.out.println(w(arr, 1, 5));
} }
Output is 3
. Thanks!
答案1
得分: 1
这段代码在给定的两个索引“start”和“k”之间找到最大元素的索引。
根据给定的输入,在索引“1”和“5”之间,子数组为“3,2,8,3,6”,其中最大的元素是“8”,它在原数组中的索引为“3”。
英文:
The code finds the index of the biggest element between given two indexes, start
and k
.
With the given input, between the indexes 1
and 5
, the subarray is 3,2,8,3,6
, and the biggest element among them is 8
, which has the index 3
in the original array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论