📜  使用堆栈反转数字

📅  最后修改于: 2021-04-28 14:16:32             🧑  作者: Mango

给定一个数字,编写一个程序以使用堆栈反转该数字。

例子:

Input : 365
Output : 563

Input : 6899
Output : 9986

我们已经在这篇文章中讨论了简单的方法来反转数字。在这篇文章中,我们将讨论如何使用堆栈反转数字。

这样做的想法是提取数字的位数并将其压入堆栈。一旦将数字的所有数字都压入堆栈,我们将开始一个接一个地弹出堆栈的内容并形成一个数字。
由于堆栈是一种LIFO数据结构,因此,新形成的数字的数字将以相反的顺序排列。

下面是上述想法的实现:

C++
// CPP program to reverse the number 
// using a stack
  
#include 
using namespace std;
  
// Stack to maintain order of digits
stack  st;
  
// Function to push digits into stack
void push_digits(int number)
{
    while (number != 0) 
    {
        st.push(number % 10);
        number = number / 10;
    }
}
  
// Function to reverse the number
int reverse_number(int number)
{
    // Function call to push number's 
    // digits to stack
    push_digits(number);
      
    int reverse = 0;
    int i = 1;
      
    // Popping the digits and forming 
    // the reversed number
    while (!st.empty()) 
    {
        reverse = reverse + (st.top() * i);
        st.pop();
        i = i * 10;
    }
      
    // Return the reversed number formed
    return reverse;
}
  
// Driver program to test above function
int main()
{
    int number = 39997;
      
    // Function call to reverse number
    cout << reverse_number(number);
      
    return 0;
}


Java
// Java program to reverse the number
// using a stack
import java.util.Stack;
  
public class GFG
{
    // Stack to maintain order of digits
    static Stack st= new Stack<>();
  
    // Function to push digits into stack
    static void push_digits(int number)
    {
        while(number != 0)
        {
            st.push(number % 10);
            number = number / 10;
        }
    }
  
    // Function to reverse the number
    static int reverse_number(int number)
    {
        // Function call to push number's
        // digits to stack
        push_digits(number);
        int reverse = 0;
        int i = 1;
  
        // Popping the digits and forming
        // the reversed number
        while (!st.isEmpty())
        {
            reverse = reverse + (st.peek() * i);
            st.pop();
            i = i * 10;
        }
  
        // Return the reversed number formed
        return reverse;
    }
  
    // Driver program to test above function
    public static void main(String[] args)
    {
        int number = 39997;
        System.out.println(reverse_number(number));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Python3 program to reverse the 
# number using a stack
  
# Stack to maintain order of digits
st = [];
  
# Function to push digits into stack
def push_digits(number):
  
    while (number != 0): 
        st.append(number % 10);
        number = int(number / 10);
  
# Function to reverse the number
def reverse_number(number):
      
    # Function call to push number's 
    # digits to stack
    push_digits(number);
      
    reverse = 0;
    i = 1;
      
    # Popping the digits and forming 
    # the reversed number
    while (len(st) > 0): 
        reverse = reverse + (st[len(st) - 1] * i);
        st.pop();
        i = i * 10;
      
    # Return the reversed number formed
    return reverse;
  
# Driver Code
number = 39997;
  
# Function call to reverse number
print(reverse_number(number));
  
# This code is contributed by mits


C#
// C# program to reverse the number 
// using a stack 
using System;
using System.Collections.Generic;
  
class GFG
{
// Stack to maintain order of digits 
public static Stack st = new Stack();
  
// Function to push digits into stack 
public static void push_digits(int number)
{
    while (number != 0)
    {
        st.Push(number % 10);
        number = number / 10;
    }
}
  
// Function to reverse the number 
public static int reverse_number(int number)
{
    // Function call to push number's 
    // digits to stack 
    push_digits(number);
    int reverse = 0;
    int i = 1;
  
    // Popping the digits and forming 
    // the reversed number 
    while (st.Count > 0)
    {
        reverse = reverse + (st.Peek() * i);
        st.Pop();
        i = i * 10;
    }
  
    // Return the reversed number formed 
    return reverse;
}
  
// Driver Code
public static void Main(string[] args)
{
    int number = 39997;
    Console.WriteLine(reverse_number(number));
}
}
  
// This code is contributed by Shrikant13


PHP


输出:

79993

时间复杂度: O(logN)
辅助空间: O(logN),其中N是输入数字。