Dado un entero N positivo de 32 bits , la tarea es encontrar el máximo entre el valor de N y el número obtenido por la representación decimal de la inversión de la representación binaria de N en un entero de 32 bits.
Ejemplos:
Entrada: N = 6
Salida: 1610612736
Explicación:
La representación binaria de 6 en un entero de 32 bits es 0000000000000000000000000000110 es decir, (000000000000000000000000000000110) 2 = (6) 10 .
Invertir esta string binaria da (011000000000000000000000000000000) 2 = (1610612736) 10 .
El máximo entre N y el número obtenido es 1610612736. Por lo tanto, imprima 1610612736.Entrada: N = 1610612736
Salida: 1610612736
Enfoque: siga los pasos a continuación para resolver el problema:
- Calcula la string binaria del número N y guárdala en una string S.
- Invierta la string S .
- Calcule el valor decimal de la string S recién invertida en una variable, digamos M .
- Después de completar los pasos anteriores, imprima el valor máximo de N y M como resultado.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> 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
<script> // JavaScript program for the above approach // Function that obtains the number // using said operations from N function reverseBin(N) { // Stores the binary representation // of the number N let S = ""; let i; // Find the binary representation // of the number N for (i = 0; i < 32; i++) { // Check for the set bits if (N & (1 << i)) S += '1'; else S += '0'; } // Reverse the string S S = S.split("").reverse().join(""); // Stores the obtained number let 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 function maximumOfTwo(N) { let M = reverseBin(N); return Math.max(N, M); } // Driver Code let N = 6; document.write(maximumOfTwo(N)); </script>
1610612736
Complejidad de Tiempo: O(32)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por tmprofessor y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA