📜  数字的每个数字的最接近的2的小数次幂

📅  最后修改于: 2021-04-17 17:15:13             🧑  作者: Mango

给定一个整数num ,数字中每个数字的任务是找到不超过该数字的2的最高幂。

例子:

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

  1. 将数字转换为其等效的字符串。
  2. 遍历字符串。
  3. 如果数字为“ 0” ,则打印0
  4. 否则,对于每个数字x ,计算2 (log 2 (x))

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the nearest power of
// two for every digit of a given number
void highestPowerOfTwo(int num)
{
    // Converting number to string
    string s = to_string(num);
 
    // Traverse the array
    for (int i = 0; i < (int)s.size();
         i++) {
 
        if (s[i] == '0') {
            cout << "0";
            continue;
        }
 
        // Calculate log base 2
        // of the current digit s[i]
        int lg = log2(int(s[i]) - 48);
 
        // Highest power of 2 <= s[i]
        int p = pow(2, lg);
 
        // ASCII conversion
        cout << char(p + 48);
    }
}
 
// Driver Code
int main()
{
    int num = 4317;
    highestPowerOfTwo(num);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to find the nearest power of
  // two for every digit of a given number
  static void highestPowerOfTwo(int num)
  {
 
    // Converting number to string
    String s = Integer.toString(num);
 
    // Traverse the array
    for (int i = 0; i < (int)s.length(); i++)
    {
 
      if (s.charAt(i) == '0')
      {
        System.out.print("0");
        continue;
      }
 
      // Calculate log base 2
      // of the current digit s[i]
      int lg
        = (int)(Math.log(s.charAt(i) - '0') / Math.log(2));
 
      // Highest power of 2 <= s[i]
      int p = (int)Math.pow(2, lg);
 
      // ASCII conversion
      System.out.print((char)(p + 48));
    }
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int num = 4317;
    highestPowerOfTwo(num);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python 3 program for the above approach
import math
 
# Function to find the nearest power of
# two for every digit of a given number
def highestPowerOfTwo(num) :
     
    # Converting number to string
    s = str(num)
 
    # Traverse the array
    for i in range(len(s)):
        if (s[i] == '0') :
            print("0")
            continue
         
        # Calculate log base 2
        # of the current digit s[i]
        lg = int(math.log2(ord(s[i]) - 48))
 
        # Highest power of 2 <= s[i]
        p = pow(2, lg)
 
        # ASCII conversion
        print(chr(p + 48), end = "")
     
# Driver Code
num = 4317
highestPowerOfTwo(num)
 
# This code is contributed by code_hunt.


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
    // Function to find the nearest power of
    // two for every digit of a given number
    static void highestPowerOfTwo(int num)
    {
        // Converting number to string
        String s = num.ToString();
 
        // Traverse the array
        for (int i = 0; i < (int)s.Length; i++)
        {
 
            if (s[i] == '0')
            {
                Console.Write("0");
                continue;
            }
 
            // Calculate log base 2
            // of the current digit s[i]
            int lg
                = (int)(Math.Log(s[i] - '0') / Math.Log(2));
 
            // Highest power of 2 <= s[i]
            int p = (int)Math.Pow(2, lg);
 
            // ASCII conversion
            Console.Write((char)(p + 48));
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int num = 4317;
        highestPowerOfTwo(num);
    }
}
 
// This code is contributed by subhammahato348.


输出:
4214

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