📜  求 N 和通过反转 N 的 32 位二进制表示形成的数之间的最大值

📅  最后修改于: 2022-05-13 01:56:10.083000             🧑  作者: Mango

求 N 和通过反转 N 的 32 位二进制表示形成的数之间的最大值

给定一个正的 32 位整数N ,任务是找到N 的值与通过将 N二进制表示反转为 32 位整数的十进制表示获得的数之间的最大值。

例子:

解决方法:按照以下步骤解决问题:

  • 计算数字N的二进制字符串并将其存储在字符串S中。
  • 反转字符串S
  • 计算变量中新反转的字符串S的十进制值,例如M
  • 完成上述步骤后,打印NM的最大值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function that obtains the number
// using said operations from N
int reverseBin(int N)
{
    // Stores the binary representation
    // of the number N
    string S = "";
    int i;
 
    // Find the binary representation
    // of the number N
    for (i = 0; i < 32; i++) {
 
        // Check for the set bits
        if (N & (1LL << i))
            S += '1';
        else
            S += '0';
    }
 
    // Reverse the string S
    reverse(S.begin(), S.end());
 
    // Stores the obtained number
    int M = 0;
 
    // Calculating the decimal value
    for (i = 0; i < 32; i++) {
 
        // Check for set bits
        if (S[i] == '1')
            M += (1LL << i);
    }
    return M;
}
 
// Function to find the maximum value
// between N and the obtained number
int maximumOfTwo(int N)
{
    int M = reverseBin(N);
 
    return max(N, M);
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << maximumOfTwo(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function that obtains the number
    // using said operations from N
    static int reverseBin(int N)
    {
        // Stores the binary representation
        // of the number N
        String S = "";
        int i;
 
        // Find the binary representation
        // of the number N
        for (i = 0; i < 32; i++) {
 
            // Check for the set bits
            if ((N & (1L << i)) != 0)
                S += '1';
            else
                S += '0';
        }
 
        // Reverse the string S
        S = (new StringBuilder(S)).reverse().toString();
 
        // Stores the obtained number
        int M = 0;
 
        // Calculating the decimal value
        for (i = 0; i < 32; i++) {
 
            // Check for set bits
            if (S.charAt(i) == '1')
                M += (1L << i);
        }
        return M;
    }
 
    // Function to find the maximum value
    // between N and the obtained number
    static int maximumOfTwo(int N)
    {
        int M = reverseBin(N);
 
        return Math.max(N, M);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int N = 6;
        System.out.print(maximumOfTwo(N));
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function that obtains the number
# using said operations from N
def reverseBin(N):
     
    # Stores the binary representation
    # of the number N
    S = ""
    i = 0
 
    # Find the binary representation
    # of the number N
    for i in range(32):
         
        # Check for the set bits
        if (N & (1 << i)):
            S += '1'
        else:
            S += '0'
 
    # Reverse the string S
    S = list(S)
    S = S[::-1]
    S = ''.join(S)
     
    # Stores the obtained number
    M = 0
 
    # Calculating the decimal value
    for i in range(32):
         
        # Check for set bits
        if (S[i] == '1'):
            M += (1 << i)
             
    return M
 
# Function to find the maximum value
# between N and the obtained number
def maximumOfTwo(N):
     
    M = reverseBin(N)
 
    return max(N, M)
 
# Driver Code
if __name__ == '__main__':
     
    N = 6
    print(maximumOfTwo(N))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
static string ReverseString(string s)
    {
        char[] array = s.ToCharArray();
        Array.Reverse(array);
        return new string(array);
    }
   
// Function that obtains the number
// using said operations from N
public static int reverseBin(int N)
{
   
    // Stores the binary representation
    // of the number N
    string S = "";
    int i;
 
    // Find the binary representation
    // of the number N
    for (i = 0; i < 32; i++) {
 
        // Check for the set bits
        if ((N & (1L << i)) != 0)
            S += '1';
        else
            S += '0';
    }
 
    // Reverse the string S
    S = ReverseString(S);
     
 
    // Stores the obtained number
    int M = 0;
 
    // Calculating the decimal value
    for (i = 0; i < 32; i++) {
 
        // Check for set bits
        if (S[i] == '1')
            M +=  (1 << i);
    }
    return M;
}
 
// Function to find the maximum value
// between N and the obtained number
static int maximumOfTwo(int N)
{
    int M = reverseBin(N);
 
    return Math.Max(N, M);
}
 
// Driver Code
static void Main()
{
    int N = 6;
    Console.Write(maximumOfTwo(N));
 
}
}
 
// This code is contributed by SoumikMondal


Javascript


输出:
1610612736

时间复杂度: O(32)
辅助空间: O(1)