Using Recursion to reverse an integer without the use of strings

后端 未结 5 448
逝去的感伤
逝去的感伤 2020-12-20 10:22

I have been trying this for some time now but could not get it to work. I am trying to have a method to reverse an integer without the use of strings or arrays. For example,

相关标签:
5条回答
  • 2020-12-20 10:34

    What about:

    public int RevDigs(int input) {
        if(input < 10) {
            return input;
        }
        else {
            return (input % 10) * (int) Math.pow(10, (int) Math.log10(input)) + RevDigs(input/10);
            /* here we:
               - take last digit of input
               - multiply by an adequate power of ten
                 (to set this digit in a "right place" of result)
               - add input without last digit, reversed
            */
        }
    }
    

    This assumes input >= 0, of course.

    0 讨论(0)
  • 2020-12-20 10:40

    How about using two methods

    public static long reverse(long n) {
        return reverse(n, 0);
    }
    
    private static long reverse(long n, long m) {
        return n == 0 ? m : reverse(n / 10, m * 10 +  n % 10);
    }
    
    public static void main(String... ignored) {
        System.out.println(reverse(123456789));
    }
    

    prints

    987654321
    
    0 讨论(0)
  • 2020-12-20 10:40
    import java.io.*;
    
    public class ReversalOfNumber {
        public static int sum =0;
        public static void main(String args []) throws IOException
        {
            System.out.println("Enter a number to get Reverse & Press Enter Button");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String input = reader.readLine();
            int number = Integer.parseInt(input);
            int revNumber = reverse(number);
            System.out.println("Reverse of "+number+" is: "+revNumber);
        }
        public static int reverse(int n)
        {       
            int unit;
            if (n>0)
            {
                unit = n % 10;
                sum= (sum*10)+unit;
                n=n/10;
                reverse(n);
            }
            return sum;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 10:55
    package Test;
    
    public class Recursive {
        int i=1;
        int multiple=10;
        int reqnum=0;
        public int recur(int no){
            int reminder, revno;
    
            if (no/10==0) {reqnum=no;
            System.out.println(" reqnum "+reqnum);
            return reqnum;}
            reminder=no%10;
            //multiple =multiple * 10;
            System.out.println(i+" i multiple "+multiple+" Reminder "+reminder+" no "+no+" reqnum "+reqnum);
            i++;
    
            no=recur(no/10);
            reqnum=reqnum+(reminder*multiple);
            multiple =multiple * 10;
            System.out.println(i+" i multiple "+multiple+" Reminder "+reminder+" no "+no+" reqnum "+reqnum);
            return reqnum;
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int num=123456789;
    
            Recursive r= new Recursive();
            System.out.println(r.recur(num));
        }
    
    }
    
    0 讨论(0)
  • 2020-12-20 11:00

    The key to using recursion is to notice that the problem you're trying to solve contains a smaller instance of the same problem. Here, if you're trying to reverse the number 13579, you might notice that you can make it a smaller problem by reversing 3579 (the same problem but smaller), multiplying the result by 10, and adding 1 (the digit you took off). Or you could reverse the number 1357 (recursively), giving 7531, then add 9 * (some power of 10) to the result. The first tricky thing is that you have to know when to stop (when you have a 1-digit number). The second thing is that for this problem, you'll have to figure out how many digits the number is so that you can get the power of 10 right. You could use Math.log10, or you could use a loop where you start with 1 and multiply by 10 until it's greater than your number.

    0 讨论(0)
提交回复
热议问题