在Java中的递归(包括数组)

huangapple go评论82阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月18日 02:15:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64405798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定