简体   繁体   English

递归方法,打印的数字大于或等于第一个数字且低于最后一个数字

[英]Recursive method that prints all of the digits in a number that or greater than the first digit and lower than the last digit

The problem: 问题:

I'm trying to write a recursive method that prints all of the digits in a number that or greater than the first digit and lower than the last digit. 我正在尝试编写一个递归方法,该方法打印的数字大于或等于第一个数字并且低于最后一个数字。 I accomplished to write a recursive method that prints all of the digits that or lower then the last digit. 我完成了写一个递归方法,打印所有数字或低于最后一位数字。 I can't figure out how to check if the digit is grater then the first digit. 我无法弄清楚如何检查数字是否比第一个数字更重要。

Examples: 例子:

For print(325648) it will print 5, 6, 4. 对于打印(325648),它将打印5,6,4。

For print(237) it will print 3. 对于打印(237),它将打印3。

For print(925648) it will not print any digit. 对于打印(925648),它不会打印任何数字。

This is my code: 这是我的代码:

public static void print(int n) {
     print(n,n%10);
}
private static void print(int n,int lastDigit) {
    if(n==0)
        return;
    if(n%10<lastDigit)
        System.out.println(n%10);
    print(n/10,lastDigit);
}

The requirement of this method: 这种方法的要求:

  • Not allowed to use loops(or methods with loops). 不允许使用循环(或带循环的方法)。
  • Allowed to use only one recursive transition. 允许仅使用一个递归转换。
  • The length of the number is not known. 数字的长度是未知的。
  • The method can change the number, but at the end of the operation the number should be same as at beginning. 该方法可以改变数字,但在操作结束时,数字应该与开始时相同。

Please note: This is not a homework assignment! 请注意:这不是家庭作业! I'm writing this method as a practice to a exam that i am talking tomorrow. 我正在写这种方法作为我明天在说的考试的练习。

The idea here is to recurse by dividing by 10 until the number is reduced to its first digit. 这里的想法是通过除以10递归,直到数字减少到第一个数字。 On the return through all recursions you have the first digit as a return value and compare quite easily. 在返回所有递归时,您将第一个数字作为返回值并进行比较。

private static void print( int n ){
    print( n/10, n%10 );
}

private static int print( int n, int ld ){
    if( n < 10 ) return n;
    int digit = n % 10;
    int first = print( n/10, ld );
    if( digit < ld && digit > first )
        System.out.println( digit );
    return first;
}
public static int firstDigit(int n) {
    if(n == 0) {
        return 0;
    }
    if(n != 0 && n / 10 == 0) {
        return n;
    }

    return firstDigit(n/10);
}

public static void print(int n) {
     print(n, firstDigit(n), n%10);
}

private static void print(int n, int firstDigit, int lastDigit) {
    if(n == 0)
        return;
    if(n % 10 < lastDigit && n % 10 > firstDigit)
        System.out.println(n%10);

    print(n/10, firstDigit, lastDigit);
}

Pass the first digit as a parameter. 将第一个数字作为参数传递。

public static int firstDigit(int n) {
    return (int) (n / Math.pow(10, Math.floor(Math.log10(n))));
}

public static void print(int n) {
     print(n, firstDigit(n), n%10);
}

private static void print(int n, int firstDigit, int lastDigit) {
    if(n == 0)
        return;
    if(n % 10 < lastDigit && n % 10 > firstDigit)
        System.out.println(n%10);

    print(n/10, lastDigit);
}

EDIT: You can calculate the first digit through recursion too: 编辑:您也可以通过递归计算第一个数字:

public static int firstDigit(int n) {
    if (n / 10 == 0) {
        return n;
    } else {
        return firstDigit(n / 10);
    }
}

Very simple, almost your code 非常简单,几乎是你的代码

public static void print(int n) {
         int first = Integer.parseInt(Integer.toString(n).substring(0, 1));
     print(n, first, n%10);
}
private static void print(int n, int first, int lastDigit) {
    if(n==0)
        return;

    if(n%10<lastDigit && n%10 > first)
        System.out.println(n%10);
    print(n/10, first, lastDigit);
}

An alternative solution is to represent the number to be checked as string: 另一种解决方案是将要检查的数字表示为字符串:

public static void main(String[] args) throws IOException {

    int n = 325648;
    print(n);
}

private static void print(int n) {

    String intAsString = String.valueOf(n);
    int numberLength = intAsString.length();
    if( numberLength < 3) {
        System.out.println("Can't do with ints shorted than 3 digits");
    }

    int firstDigit = Integer.valueOf(intAsString.substring(0,1));
    int lastDigit = Integer.valueOf(intAsString.substring(numberLength-1,numberLength));
    print(intAsString, 0, firstDigit, lastDigit  );
}

private static void print(String intAsString,int index, int firstDigit, int lastDigit) {

    int digit = Integer.valueOf(intAsString.substring(index,index+1));

    if((digit > firstDigit) && (digit < lastDigit)) {
        System.out.print(digit);
    }

    if((index +1) < intAsString.length()) {
        print(intAsString,++index, firstDigit, lastDigit);
    }
}

I just tried with string and array compare, I using string because I want code to be bit flexible. 我只是尝试使用字符串和数组比较,我使用字符串,因为我希望代码有点灵活。

public class Data {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        print(3245);
    }

    static void print(int n){
        String s = n+"";
        String[] s1 = s.split("");

        printF(Integer.valueOf(s1[0]),Integer.valueOf(s1[s1.length-1]),s1,0);
    }

    static void printF(int lst,int end, String[] ar,int index){

        if(Integer.valueOf(ar[index])>lst && Integer.valueOf(ar[index])<end){
            System.out.println(ar[index]);
        }

        if(index < ar.length-1){
            index = index+1;
            Data.printF(lst,end, ar,index);
        }

    }
}

Following the test output of above written code: 按照上面编写的代码的测试输出:

Input: 3245
output: 4

Input:1234567
output:23456

Input:58697
output:8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 递归数字的数字总和(直到数字小于10)只有java 8 lambdas - Recursive Sum of digits of number(until digit is less than 10) java 8 lambdas only 如何检查一个数字的每个数字是否大于或等于另一个数字? - How to check every digit of a number is greater or equal than another number? 打印小于或等于56且数字总和大于10的两位数 JAVA - print two digit numbers which are less than or equal to 56 and the sum of digits is greater than 10 JAVA 找出前两位数字和后两位数字之和的平方等于数字本身的所有四位数字 - find all four digit numbers for which the square of the sum of the first two digits and the last two digits equal the number itself 逐行打印数字位数的递归方法 - Recursive method that prints the digits of the number line by line 使用递归方法计算位数 - count number of digit using recursive method 使用递归显示数字的所有数字时无法打印数字的最后一位? - Can't print last digit of a number while displaying all digits of a number using recursion? 如何从java中超过3位数的数字中获得可能的3位数组合 - How can get the possible 3 digit combination from a number having more than 3 digits in java output 不正确(交换数字的第一位和最后一位) - output is not correct(Swapping the first and last digit of a number) 允许字符串中除9位和16位以外的任何数字 - Allow any digit in a string other than 9 and 16 digits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM