如何使用递归使用单个变量反转java中的数字?

时间:2017-03-16 12:59:17

标签: java

//Is there any simple idea other than it?
static int rev(int n){
    if (n==0)return 0;
    else return n%10^10+rev(n/10)   
}

我想用单变量和递归来反转数字。

1 个答案:

答案 0 :(得分:0)

//Here i got the idea.
public static void main(String[] args) {
    // Let the number be 139.
    int n=139;
    System.out.println("reverse is "+rev(n));
}
static int rev(int n){
    if (n<10)return n;
    else return n%10*(int) Math.pow(10,(int)Math.log10(n))+rev(n/10);
        //Math class is used to return the number of digits and power.
}

这给出:: reverse是931