给定一个整数num ,数字中每个数字的任务是找到不超过该数字的2的最高幂。
例子:
Input: num = 4317
Output: 4214
Explanation:
The highest power of 2 ≤ 4 is 4.
The highest power of 2 ≤ 3 is 2.
The highest power of 2 ≤ 1 is 1.
The highest power of 2 ≤ 7 is 4.
Input: num = 8015
Output: 8014
方法:请按照以下步骤解决问题:
- 将数字转换为其等效的字符串。
- 遍历字符串。
- 如果数字为“ 0” ,则打印0 。
- 否则,对于每个数字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)