📌  相关文章
📜  可以通过交换不同奇偶校验的相邻数字获得的最小整数

📅  最后修改于: 2021-04-27 17:40:06             🧑  作者: Mango

给定一个整数N ,任务是找到可以从给定整数获得的最小整数,以便可以任意次数交换不同奇偶校验位的相邻数字。
奇偶校验的两位数字意味着它们被二除时将具有不同的余数。

例子:

方法:方法的想法是使用两个堆栈来保留数字的位数。

  • 在一个堆栈中,可以存储可被二整除的数字。
  • 在另一个堆栈中,可以存储不能被二整除的数字。
  • 然后将元素从数字的相反顺序推入两个堆栈中。
  • 现在,将弹出堆栈中其顶部包含较小元素的元素,并将其与答案连接,直到其中一个堆栈为空。
  • 然后将堆栈中其余的元素连接起来,该元素与答案不为空,最后返回输出。

下面是上述方法的实现。

CPP
// C++ implementation of the above approach.
#include 
using namespace std;
// Function to return the minimum number
int minimumNo(int n)
{
    int ans = 0;
    stack stack1;
    stack stack2;
    while (n != 0) {
        int r = n % 10;
  
        // Store the elements which are
        // divisible by two in stack1
        if (r % 2 == 0) {
            stack1.push(r);
        }
  
        // Store the elements which are
        // not divisible by two in stack2.
        else {
            stack2.push(r);
        }
        n = n / 10;
    }
  
    while (!stack1.empty() && !stack2.empty()) {
  
        // Concatenate the answer with smaller value
        // of the topmost elements of both the
        // stacks and then pop that element
        if (stack1.top() < stack2.top()) {
            ans = ans * 10 + stack1.top();
            stack1.pop();
        }
        else {
            ans = ans * 10 + stack2.top();
            stack2.pop();
        }
    }
  
    // Concatenate the answer with remaining 
    // values of stack1.
    while (!stack1.empty()) {
        ans = ans * 10 + stack1.top();
        stack1.pop();
    }
  
    // Concatenate the answer with remaining 
    // values of stack2.
    while (!stack2.empty()) {
        ans = ans * 10 + stack2.top();
        stack2.pop();
    }
    return ans;
}
  
// Driver code
int main()
{
    int n1 = 64432;
  
    // Function calling
    cout << minimumNo(n1) << endl;
  
    int n2 = 3137;
    cout << minimumNo(n2) << endl;
  
    return 0;
}


Java
// Java implementation of the above approach.
import java.util.*;
  
class GFG
{
      
// Function to return the minimum number
static int minimumNo(int n)
{
    int ans = 0;
    Stack stack1 = new Stack();
    Stack stack2 = new Stack();
    while (n != 0)
    {
        int r = n % 10;
  
        // Store the elements which are
        // divisible by two in stack1
        if (r % 2 == 0)
        {
            stack1.add(r);
        }
  
        // Store the elements which are
        // not divisible by two in stack2.
        else 
        {
            stack2.add(r);
        }
        n = n / 10;
    }
  
    while (!stack1.isEmpty() && !stack2.isEmpty())
    {
  
        // Concatenate the answer with smaller value
        // of the topmost elements of both the
        // stacks and then pop that element
        if (stack1.peek() < stack2.peek())
        {
            ans = ans * 10 + stack1.peek();
            stack1.pop();
        }
        else 
        {
            ans = ans * 10 + stack2.peek();
            stack2.pop();
        }
    }
  
    // Concatenate the answer with remaining 
    // values of stack1.
    while (!stack1.isEmpty())
    {
        ans = ans * 10 + stack1.peek();
        stack1.pop();
    }
  
    // Concatenate the answer with remaining 
    // values of stack2.
    while (!stack2.isEmpty())
    {
        ans = ans * 10 + stack2.peek();
        stack2.pop();
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int n1 = 64432;
  
    // Function calling
    System.out.print(minimumNo(n1) + "\n");
  
    int n2 = 3137;
    System.out.print(minimumNo(n2) + "\n");
}
}
  
// This code is contributed by 29AjayKumar


Python
# Python3 implementation of the above approach.
  
# Function to return the minimum number
def minimumNo(n):
    ans = 0
    stack1 = []
    stack2 = []
    while (n != 0):
        r = n % 10
  
        # Store the elements which are
        # divisible by two in stack1
        if (r % 2 == 0):
            stack1.append(r)
  
        # Store the elements which are
        # not divisible by two in stack2.
        else :
            stack2.append(r)
  
        n = n // 10
    while (len(stack1) > 0 and len(stack2) > 0):
  
        # Concatenate the answer with smaller value
        # of the topmost elements of both the
        # stacks and then pop that element
        if (stack1[-1] < stack2[-1]):
            ans = ans * 10 + stack1[-1]
            del stack1[-1]
  
        else:
            ans = ans * 10 + stack2[-1]
            del stack2[-1]
  
    # Concatenate the answer with remaining
    # values of stack1.
    while (len(stack1) > 0):
        ans = ans * 10 + stack1[-1]
        del stack1[-1]
  
    # Concatenate the answer with remaining
    # values of stack2.
    while (len(stack2) > 0):
        ans = ans * 10 + stack2[-1]
        del stack2[-1]
  
    return ans
  
# Driver code
n1 = 64432
  
# Function calling
print(minimumNo(n1))
  
n2 = 3137
print(minimumNo(n2))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach.
using System;
using System.Collections.Generic;
  
class GFG
{
      
// Function to return the minimum number
static int minimumNo(int n)
{
    int ans = 0;
    Stack stack1 = new Stack();
    Stack stack2 = new Stack();
    while (n != 0)
    {
        int r = n % 10;
  
        // Store the elements which are
        // divisible by two in stack1
        if (r % 2 == 0)
        {
            stack1.Push(r);
        }
  
        // Store the elements which are
        // not divisible by two in stack2.
        else
        {
            stack2.Push(r);
        }
        n = n / 10;
    }
  
    while (stack1.Count != 0 && stack2.Count != 0)
    {
  
        // Concatenate the answer with smaller value
        // of the topmost elements of both the
        // stacks and then pop that element
        if (stack1.Peek() < stack2.Peek())
        {
            ans = ans * 10 + stack1.Peek();
            stack1.Pop();
        }
        else
        {
            ans = ans * 10 + stack2.Peek();
            stack2.Pop();
        }
    }
  
    // Concatenate the answer with remaining 
    // values of stack1.
    while (stack1.Count != 0)
    {
        ans = ans * 10 + stack1.Peek();
        stack1.Pop();
    }
  
    // Concatenate the answer with remaining 
    // values of stack2.
    while (stack2.Count != 0)
    {
        ans = ans * 10 + stack2.Peek();
        stack2.Pop();
    }
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
    int n1 = 64432;
  
    // Function calling
    Console.Write(minimumNo(n1) + "\n");
  
    int n2 = 3137;
    Console.Write(minimumNo(n2) + "\n");
}
}
  
// This code is contributed by PrinciRaj1992


输出:
36442
3137