java 使用递归查找整数中的最大数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16469774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:03:20  来源:igfitidea点击:

finding the largest digit in an integer using recursion

javarecursion

提问by Assaf

I have a practice who's task is to find the largest digit in an integer using recursion in java. For example, for the number 13441 the digit '4' will be returned.

我有一个练习,他的任务是使用 java 中的递归找到整数中的最大数字。例如,对于数字 13441,将返回数字“4”。

I have been trying for a day now and nothing worked.

我已经尝试了一天,但没有任何效果。

What I thought could work is the following code, which I can't quite get the "base case" for:

我认为可以工作的是以下代码,我不能完全得到“基本情况”:

public static int maxDigit(int n) {
    int max;
    if (n/100==0) {
        if (n%10>(n/10)%10) {
            max=n%10;
        }
        else
            max=(n/10)%10;
    }
    else if (n%10>n%100)
        max=n%10;
    else
        max=n%100;
    return maxDigit(n/10);
}

As you can see it's completely wrong.

如您所见,这是完全错误的。

Any help would be great. Thank you

任何帮助都会很棒。谢谢

回答by Alnitak

This works by recursively comparing the right most digit with the highest digit of the remaining digits (those being obtained by dividing the original number by 10):

这是通过递归比较最右边的数字和其余数字的最高数字(通过将原始数字除以 10 获得的数字)来实现的:

int maxDigit(int n) {
    n = Math.abs(n);   // make sure n is positive
    if (n > 0) {
        int digit = n % 10;
        int max = maxDigit(n / 10);
        return Math.max(digit, max);
    } else {
        return 0;
    } 
}

回答by Joachim Isaksson

The simplest base case, is that if n is 0, return 0.

最简单的基本情况是,如果 n 为 0,则返回 0。

public static int maxDigit(int n){
    if(n==0)                               // Base case: if n==0, return 0
        return 0;
    return Math.max(n%10, maxDigit(n/10)); // Return max of current digit and 
                                           // maxDigit of the rest 
}

or, slightly more concise;

或者,稍微简洁一些;

public static int maxDigit(int n){
    return n==0 ? 0 : Math.max(n%10, maxDigit(n/10));
}

回答by Jacob Mattison

I won't dig into your code, which I think is more complicated than it has to be. But it seems to me that the cases are actually fairly simple (unless I'm missing something):

我不会深入研究你的代码,我认为它比它必须的更复杂。但在我看来,这些案例实际上相当简单(除非我遗漏了一些东西):

base case: parameter only has one digit, return that one digit as parameter

基本情况:参数只有一位,返回那一位作为参数

general case: return whichever is higher of (the first digit in the parameter) and (the maxDigit of the remaining digits in the parameter)

一般情况:返回(参数中的第一个数字)和(参数中剩余数字的 maxDigit 中的较高者)

回答by user1606259

You may also write:

你也可以写:

public static int maxDigit(int n, int max){
    if(n!=0)    {
        if(n%10 > max) {
            max = n%10;
        }
        return maxDigit(n/10, max);
    }
    return max;
}