📌  相关文章
📜  将数组元素的绝对差值和四舍五入值串联为二进制字符串的十进制等效值

📅  最后修改于: 2021-09-06 17:45:13             🧑  作者: Mango

给定一个由N 个浮点数组成的数组arr[] ,任务是打印二进制数组的十进制表示,该二进制数组由每个数组元素的下限和舍入值之间的绝对差构成。

例子:

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

  • 初始化一个变量,比如结果0 ,它存储形成的结果数字。
  • 初始化一个变量,比如power0 ,它保持在每一步中添加2的幂。
  • 从末尾遍历给定数组arr[]并执行以下步骤:
    • 初始化一个变量,比如存储每个数组元素的舍入值和下限值之间的绝对差值的
    • 如果绝对差的值为1 ,则将该数字乘以2的适当并将其添加到变量result 中
    • power的值增加1
  • 完成上述步骤后,将结果值打印为所需的二进制表示的十进制等值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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.


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


输出:
42

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live