简体   繁体   中英

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.

For print(237) it will print 3.

For print(925648) it will not print any digit.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM