0
//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)
}
我想用單變量和遞歸來反轉數字。如何使用遞歸在單個變量中反轉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)
}
我想用單變量和遞歸來反轉數字。如何使用遞歸在單個變量中反轉java中的數字?
//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.
}
這給::反向是931
http://stackoverflow.com/questions/20670444/using-recursion-to-reverse-an-integer-without-the-use-of-strings – Venkat
我後來看到相同的答案,但我只是要求用單個變量來逆轉數字。 –