Dada una array arr[] que consta de N números de punto flotante , la tarea es imprimir la representación decimal de la array binaria construida a partir de la diferencia absoluta entre el valor mínimo y el valor de redondeo para cada elemento de la array.
Ejemplos:
Entrada: arr[] = {1.2, 2.6, 4.2, 6.9, 3.1, 21.6, 91.2}
Salida: 42
Explicación:
A continuación se muestra la imagen para ilustrar el ejemplo anterior:Entrada: arr[] = {5.7, 2.8, 1.9, 5.6, 2.2}
Salida: 30
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos resultado como 0 que almacena los números resultantes formados.
- Inicialice una variable, digamos potencia como 0 que mantiene la potencia de 2 agregada en cada paso.
- Recorra la array dada arr[] desde el final y realice los siguientes pasos:
- Inicialice una variable, digamos un bit que almacene la diferencia absoluta entre el valor de redondeo y el valor mínimo de cada elemento de la array.
- Si el valor de la diferencia absoluta es 1 , entonces multiplique el dígito con la potencia adecuada de 2 y súmelo al resultado variable .
- Incrementa el valor de la potencia en 1 .
- Después de completar los pasos anteriores, imprima el valor del resultado como el equivalente decimal requerido de la representación binaria .
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 to find the decimal equivalent // of the new binary array constructed // from absolute decimal of floor and // the round-off values int findDecimal(float arr[], int N) { int bit, power = 0, result = 0; // Traverse the givenarray from // the end for (int i = N - 1; i >= 0; i--) { // Stores the absolute difference // between floor and round-off // each array element bit = abs(floor(arr[i]) - round(arr[i])); // If bit / difference is 1, then // calculate the bit by proper // power of 2 and add it to result if (bit) result += pow(2, power); // Increment the value of power power++; } // Print the result cout << result; } // Driver Code int main() { float arr[] = { 1.2, 2.6, 4.2, 6.9, 3.1, 21.6, 91.2 }; int N = sizeof(arr) / sizeof(arr[0]); findDecimal(arr, N); return 0; }
Java
// Java program for above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to find the decimal equivalent // of the new binary array constructed // from absolute decimal of floor and // the round-off values static void findDecimal(double arr[], int N) { int bit, power = 0, result = 0; // Traverse the givenarray from // the end for (int i = N - 1; i >= 0; i--) { // Stores the absolute difference // between floor and round-off // each array element bit = Math.abs((int)Math.floor(arr[i]) - (int)Math.round(arr[i])); // If bit / difference is 1, then // calculate the bit by proper // power of 2 and add it to result if (bit != 0) result += Math.pow(2, power); // Increment the value of power power++; } // Print the result System.out.print(result); } // Driver Code public static void main(String[] args) { double arr[] = { 1.2, 2.6, 4.2, 6.9, 3.1, 21.6, 91.2 }; int N = arr.length; findDecimal(arr, N); } } // This code is contributed by souravghosh0416.
Python3
# Python program for the above approach # Function to find the decimal equivalent # of the new binary array constructed # from absolute decimal of floor and # the round-off values def findDecimal(arr, N): power = 0; result = 0; # Traverse the givenarray from # the end for i in range(N - 1, -1, -1): # Stores the absolute difference # between floor and round-off # each array element bit = abs(int(arr[i]) - round(arr[i])); # If bit / difference is 1, then # calculate the bit by proper # power of 2 and add it to result if (bit): result += pow(2, power); # Increment the value of power power += 1; # Print the result print(result); # Driver Code arr = [ 1.2, 2.6, 4.2, 6.9, 3.1, 21.6, 91.2 ]; N = len(arr) findDecimal(arr, N); # This code is contributed by gfgking.
C#
// C# program for the above approach using System; class GFG{ // Function to find the decimal equivalent // of the new binary array constructed // from absolute decimal of floor and // the round-off values static void findDecimal(double[] arr, int N) { int bit, power = 0, result = 0; // Traverse the givenarray from // the end for(int i = N - 1; i >= 0; i--) { // Stores the absolute difference // between floor and round-off // each array element bit = Math.Abs((int)Math.Floor(arr[i]) - (int)Math.Round(arr[i])); // If bit / difference is 1, then // calculate the bit by proper // power of 2 and add it to result if (bit != 0) result += (int)Math.Pow(2, power); // Increment the value of power power++; } // Print the result Console.WriteLine(result); } // Driver Code public static void Main() { double[] arr = { 1.2, 2.6, 4.2, 6.9, 3.1, 21.6, 91.2 }; int N = arr.Length; findDecimal(arr, N); } } // This code is contriobuted by sanjoy_62
Javascript
<script> // JavaScript program for the above approach // Function to find the decimal equivalent // of the new binary array constructed // from absolute decimal of floor and // the round-off values function findDecimal(arr, N) { let bit, power = 0, result = 0; // Traverse the givenarray from // the end for (let i = N - 1; i >= 0; i--) { // Stores the absolute difference // between floor and round-off // each array element bit = Math.abs(Math.floor(arr[i]) - Math.round(arr[i])); // If bit / difference is 1, then // calculate the bit by proper // power of 2 and add it to result if (bit != 0) result += Math.pow(2, power); // Increment the value of power power++; } // Print the result document.write(result); } // Driver Code let arr = [1.2, 2.6, 4.2, 6.9, 3.1, 21.6, 91.2]; let N = arr.length; findDecimal(arr, N); // This code is contributed by Hritik </script>
42
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por yuvraj_chandra y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA