📌  相关文章
📜  大于或等于N的最小数字,这样就不会设置奇数位

📅  最后修改于: 2021-05-06 07:25:28             🧑  作者: Mango

给定一个整数N ,任务是找到最小的整数X ,使其不设置奇数位并且X≥N
注意:假定从右侧开始定位位,并且假定第一位为0位。

例子:

方法:可以使用贪婪方法和一些位属性来解决问题。如果将较小的2的幂恰好一次并相加,则它们永远不能超过较高的2的幂(例如(1 + 2 + 4)<8)。以下贪婪方法用于解决上述问题:

  • 最初计算位数。
  • 获取设置位的最左索引。
  • 如果最左边的设置位处于奇数索引,则答案将始终为(1 <<(leftmost_bit_index + 1))
  • 否则,通过将所有偶数位设置为0到leftmost_bit_index,贪婪地形成一个数字。现在从右边贪婪地删除2的幂以检查我们是否得到满足指定条件的数字。
  • 如果上述条件不能提供我们的电话号码,那么我们只需设置下一个最左边的偶数位并返回答案。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to count the total bits
int countBits(int n)
{
    int count = 0;
  
    // Iterate and find the
    // number of set bits
    while (n) {
        count++;
  
        // Right shift the number by 1
        n >>= 1;
    }
    return count;
}
  
// Function to find the nearest number
int findNearestNumber(int n)
{
  
    // Count the total number of bits
    int cnt = countBits(n);
  
    // To get the position
    cnt -= 1;
  
    // If the last set bit is
    // at odd position then
    // answer will always be a number
    // with the left bit set
    if (cnt % 2) {
        return 1 << (cnt + 1);
    }
  
    else {
  
        int tempnum = 0;
  
        // Set all the even bits which
        // are possible
        for (int i = 0; i <= cnt; i += 2)
            tempnum += 1 << i;
  
        // If the number still is less than N
        if (tempnum < n) {
  
            // Return the number by setting the
            // next even set bit
            return (1 << (cnt + 2));
        }
  
        else if (tempnum == n)
            return n;
  
        // If we have reached this position
        // it means tempsum > n
        // hence turn off even bits to get the
        // first possible number
        for (int i = 0; i <= cnt; i += 2) {
  
            // Turn off the bit
            tempnum -= (1 << i);
  
            // If it gets lower than N
            // then set it and return that number
            if (tempnum < n)
                return tempnum += (1 << i);
        }
    }
}
  
// Driver code
int main()
{
    int n = 19;
    cout << findNearestNumber(n);
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
    // Function to count the total bits
    static int countBits(int n) 
    {
        int count = 0;
  
        // Iterate and find the
        // number of set bits
        while (n > 0)
        {
            count++;
  
            // Right shift the number by 1
            n >>= 1;
        }
        return count;
    }
  
    // Function to find the nearest number
    static int findNearestNumber(int n) 
    {
  
        // Count the total number of bits
        int cnt = countBits(n);
  
        // To get the position
        cnt -= 1;
  
        // If the last set bit is
        // at odd position then
        // answer will always be a number
        // with the left bit set
        if (cnt % 2 == 1) 
        {
            return 1 << (cnt + 1);
        } 
        else 
        {
  
            int tempnum = 0;
  
            // Set all the even bits which
            // are possible
            for (int i = 0; i <= cnt; i += 2) 
            {
                tempnum += 1 << i;
            }
  
            // If the number still is less than N
            if (tempnum < n) 
            {
  
                // Return the number by setting the
                // next even set bit
                return (1 << (cnt + 2));
            } 
            else
            if (tempnum == n)
            {
                return n;
            }
  
            // If we have reached this position
            // it means tempsum > n
            // hence turn off even bits to get the
            // first possible number
            for (int i = 0; i <= cnt; i += 2)
            {
  
                // Turn off the bit
                tempnum -= (1 << i);
  
                // If it gets lower than N
                // then set it and return that number
                if (tempnum < n) 
                {
                    return tempnum += (1 << i);
                }
            }
        }
        return Integer.MIN_VALUE;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int n = 19;
  
        System.out.println(findNearestNumber(n));
    }
}
  
// This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to count the total bits
    static int countBits(int n) 
    {
        int count = 0;
  
        // Iterate and find the
        // number of set bits
        while (n > 0)
        {
            count++;
  
            // Right shift the number by 1
            n >>= 1;
        }
        return count;
    }
  
    // Function to find the nearest number
    static int findNearestNumber(int n) 
    {
  
        // Count the total number of bits
        int cnt = countBits(n);
  
        // To get the position
        cnt -= 1;
  
        // If the last set bit is
        // at odd position then
        // answer will always be a number
        // with the left bit set
        if (cnt % 2 == 1) 
        {
            return 1 << (cnt + 1);
        } 
        else
        {
  
            int tempnum = 0;
  
            // Set all the even bits which
            // are possible
            for (int i = 0; i <= cnt; i += 2) 
            {
                tempnum += 1 << i;
            }
  
            // If the number still is less than N
            if (tempnum < n) 
            {
  
                // Return the number by setting the
                // next even set bit
                return (1 << (cnt + 2));
            } 
            else
            if (tempnum == n)
            {
                return n;
            }
  
            // If we have reached this position
            // it means tempsum > n
            // hence turn off even bits to get the
            // first possible number
            for (int i = 0; i <= cnt; i += 2)
            {
  
                // Turn off the bit
                tempnum -= (1 << i);
  
                // If it gets lower than N
                // then set it and return that number
                if (tempnum < n) 
                {
                    return tempnum += (1 << i);
                }
            }
        }
        return int.MinValue;
    }
  
    // Driver code
    public static void Main() 
    {
        int n = 19;
  
        Console.WriteLine(findNearestNumber(n));
    }
}
  
// This code is contributed by anuj_67..


Python3
# Python implementation of the above approach
  
   
# Function to count the total bits
def countBits(n):
    count = 0;
   
    # Iterate and find the
    # number of set bits
    while (n>0):
        count+=1;
   
        # Right shift the number by 1
        n >>= 1;
    return count;
   
# Function to find the nearest number
def findNearestNumber(n):
   
    # Count the total number of bits
    cnt = countBits(n);
   
    # To get the position
    cnt -= 1;
   
    # If the last set bit is
    # at odd position then
    # answer will always be a number
    # with the left bit set
    if (cnt % 2):
        return 1 << (cnt + 1);
   
    else:
   
        tempnum = 0;
   
        # Set all the even bits which
        # are possible
        for i in range(0,cnt+1,2):
            tempnum += 1 << i;
   
        # If the number still is less than N
        if (tempnum < n):
   
            # Return the number by setting the
            # next even set bit
            return (1 << (cnt + 2));
   
        elif (tempnum == n):
            return n;
   
        # If we have reached this position
        # it means tempsum > n
        # hence turn off even bits to get the
        # first possible number
        for i in range(0,cnt+1,2):
   
            # Turn off the bit
            tempnum -= (1 << i);
   
            # If it gets lower than N
            # then set it and return that number
            if (tempnum < n):
                tempnum += (1 << i);
                return tempnum;
# Driver code
n = 19;
print(findNearestNumber(n));
  
# This code contributed by PrinciRaj1992


输出:
20