簡體   English   中英

使用遞歸來反轉整數而不使用字符串

[英]Using Recursion to reverse an integer without the use of strings

我已經嘗試了一段時間,但無法正常工作。 我正在嘗試一種無需使用字符串或數組即可反轉整數的方法。 例如,123應該以整數形式反轉為321。

我的第一次嘗試:

/** reverses digits of integer using recursion */
public int RevDigs(int input)
{
    int reverse = 0;
    if(input == 0)
    {
        return reverse;
    }
    int tempRev = RevDigs(input/10);
    if(tempRev >= 10)
        reverse = input%10 * (int)Math.pow(tempRev/10, 2) + tempRev;
    if(tempRev <10 && tempRev >0)
        reverse = input%10*10 + tempRev;
    if(tempRev == 0)
        reverse = input%10;   
    return reverse;
}//======================

我也嘗試使用它,但是似乎弄亂了中間數字:

/** reverses digits of integer using recursion */
public int RevDigs(int input)
{
    int reverse = 0;
    if(input == 0)
    {
        return reverse;
    }
    if(RevDigs(input/10) == 0)
        reverse = input % 10;
    else
    {
        if(RevDigs(input/10) < 10)
            reverse = (input % 10) *10 + RevDigs(input/10);
        else
            reverse = (input % 10)* 10 * (RevDigs(input/10)/10 + 1) + RevDigs(input/10);
        }
    return reverse;
}

我嘗試查看網站上的一些示例,但是無法使它們正常工作。 為了進一步說明,我不能為此項目使用字符串或數組,而必須使用遞歸。 有人可以幫我解決問題。 謝謝。

如何使用兩種方法

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));
}

版畫

987654321

關於什么:

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
        */
    }
}

當然,這假定input >= 0

使用遞歸的關鍵是要注意,您要解決的問題包含相同問題的較小實例。 在這里,如果您要反轉數字13579,您可能會注意到,可以通過反轉3579(相同的問題,但較小的),將結果乘以10,然后加1(取下的數字),使問題變小。 )。 或者,您可以將數字1357(遞歸)反轉,得到7531,然后將9 *(冪為10)加到結果中。 第一件棘手的事情是,您必須知道何時停止(當您擁有一個1位數的數字時)。 第二件事是,對於此問題,您必須找出數字的位數,以便正確獲得10的冪。 您可以使用Math.log10 ,也可以使用從1開始並乘以10直到大於數字的循環。

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));
    }

}
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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM