使用递归查找数字逆的Java程序
递归是一个函数反复调用自身直到它落入基本条件并实现我们的动机的过程。
要使用递归解决任何问题,我们只需按照以下步骤操作:
- 从类似于较大/原始问题的问题中假设较小的问题。
- 决定作为我们基本条件的最小有效输入或最小无效输入的答案。
- 接近解决方案并将答案链接到递归函数给出的较小问题,以使用它找到较大/原始问题的答案。
例子:
Input: 14689
Output: 98641
Input: 7654321
Output: 1234567
方法一:
- 在这种方法中,我们可以简单地打印一个数字的个位数字。
- 然后在去掉这个个位数后调用递归函数(number/10)
- 这个过程一直持续到数字减少到一位数。
例子:
num = 82695
reverse(82695)
|
|__print(5)
reverse(8269)
|
|__print(9)
reverse(826)
|
|__print(6)
reverse(82)
|
|__print(2)
reverse(8)
|
|__print(8)
return
Java
// Java program to reverse
// an integer recursively
class GFG {
// Recursive function to print
// the number in reversed form
public static void Reverse(int num)
{
// base condition to end recursive calls
if (num < 10) {
System.out.println(num);
return;
}
else {
// print the unit digit of the given number
System.out.print(num % 10);
// calling function for remaining number other
// than unit digit
Reverse(num / 10);
}
}
// driver code
public static void main(String args[])
{
// number to be reversed
int num = 98765;
System.out.print("Reversed Number: ");
// calling recursive function
// to print the number in
// reversed form
Reverse(num);
}
}
Java
// Java program to reverse an integer recursively
class GFG {
// Variable to store reversed
// number after every
// recursive call
static int ans = 0;
static int Reverse(int var)
{
// base condition to end the
// recursive calling of function
if (var == 0) {
// We have reversed the
// complete number and
// stored in ans variable
return ans;
}
if (var > 0) {
// temp variable to store the digit at unit
// place in the number
int temp = var % 10;
// Add this temp variable in the ans variable
// which stores the number reversed till now
ans = ans * 10 + temp;
// recursive calling of function to reverse the
// remaining number
Reverse(var / 10);
}
// returning final answer when the number is
// reversed completely
return ans;
}
public static void main(String[] args)
{
// Number to be reversed
int var = 98765;
// Variable to store reversed number returned by
// reverse function
int rev;
// Calling reverse function and storing the return
// value in rev variable
rev = Reverse(var);
// Printing the Reversed Number
System.out.println("Reversed number: " + rev);
}
}
输出
Reversed Number: 56789
方法二:
- 在这种方法中,我们可以简单地维护一个变量,我们可以在其中存储迄今为止颠倒的数字。
- 我们可以通过从数字中提取一个单位数字然后将这个提取的整数添加到相反的数字中来做到这一点
- 但这里的关键因素是,在将提取的数字与反向数字相加之前,我们必须将反向数字乘以 10。
例子:
num = 48291
ans = 0 -> variable to store reversed number
How this works:
reverse(num)
|
|__ temp = num % 10 -> extracting unit digit from nnumber
ans = ans*10 + temp -> adding temp at unit position in reversed number
reverse(num/10) -> calling function for remaining number
Implementation:
reverse(48291)
|
|__ temp=1
ans= 0*10 + 1 --> ans=1
reverse(4829)
|
|__ temp=9
ans= 1*10 + 9 --> ans=19
reverse(482)
|
|__ temp= 2
ans= 19*10 +2 --> ans=192
reverse(48)
|
|__ temp=8
ans=192*10 + 8 --> ans=1928
reverse(4)
|
|__ temp=4
ans=1928*10 +4 --> ans=19284
reverse(0)
|
|__ return ans
Java
// Java program to reverse an integer recursively
class GFG {
// Variable to store reversed
// number after every
// recursive call
static int ans = 0;
static int Reverse(int var)
{
// base condition to end the
// recursive calling of function
if (var == 0) {
// We have reversed the
// complete number and
// stored in ans variable
return ans;
}
if (var > 0) {
// temp variable to store the digit at unit
// place in the number
int temp = var % 10;
// Add this temp variable in the ans variable
// which stores the number reversed till now
ans = ans * 10 + temp;
// recursive calling of function to reverse the
// remaining number
Reverse(var / 10);
}
// returning final answer when the number is
// reversed completely
return ans;
}
public static void main(String[] args)
{
// Number to be reversed
int var = 98765;
// Variable to store reversed number returned by
// reverse function
int rev;
// Calling reverse function and storing the return
// value in rev variable
rev = Reverse(var);
// Printing the Reversed Number
System.out.println("Reversed number: " + rev);
}
}
输出
Reversed number: 56789
注意: Java在发生溢出时不会抛出异常,因此如果方法2中的倒数大于Integer.MAX_VALUE (2147483647)可能会出现溢出问题,但方法1不会出现此类问题。