📌  相关文章
📜  检查N的二进制表示形式的所有设置位是否至少相距K个位

📅  最后修改于: 2021-04-26 05:10:08             🧑  作者: Mango

给定数字NK ,检查N的二进制表示形式的所有设置位是否至少相距K个位置的任务。

例子:

Input: N = 5, K = 1 
Output: YES
Explanation: 
Binary of 5 is 101.
The 1's are 1 place far from each other.

Input: N = 10, K = 2
Output: NO
Explanation:
Binary of 10 is 1010.
The 1's are not at least 2 places far from each other.

方法:

  1. 遍历N的二进制表示形式中的所有位,并保持变量’count’初始化为0。
  2. 每当找到设置的位(1)时,请检查count <=K。如果不返回false。
  3. 如果找到未设置的位(0),则将count的值增加1。

下面是上述方法的实现。

C++
// C++ program to check if all the set
// bits of the binary representation
// of N are at least K places away.
#include 
using namespace std;
 
bool CheckBits(int N, int K)
{
    // Initialize check and count
    // with 0
    int check = 0;
    int count = 0;
 
    for (int i = 31; i >= 0; i--)
    {
 
        // The i-th bit is a set bit
        if ((1 << i) & N)
        {
 
            // This is the first set bit so,
            // start calculating all the
            // distances between consecutive
            // bits from here
            if (check == 0)
            {
                check = 1;
            }
            else
            {
                // If count is less than K
                // return false
                if (count < K)
                {
                    return false;
                }
            }
            count = 0;
        }
        else
        {
            // Adding the count as the
            // number of zeroes increase
            // between set bits
            count++;
        }
    }
 
    return true;
}
// Driver code
int main()
{
 
    int N = 5;
    int K = 1;
 
    if(CheckBits(N, K))
    {
        cout << "YES";
    }
    else
    {
        cout << "NO";
    }
 
 
 
    return 0;
}


Java
// Java program to check if all the set
// bits of the binary representation
// of N are at least K places away.
import java.util.*;
 
class GFG{
 
static boolean CheckBits(int N, int K)
{
     
    // Initialize check and count
    // with 0
    int check = 0;
    int count = 0;
 
    for(int i = 31; i >= 0; i--)
    {
         
        // The i-th bit is a set bit
        if (((1 << i) & N) > 0)
        {
             
            // This is the first set bit so,
            // start calculating all the
            // distances between consecutive
            // bits from here
            if (check == 0)
            {
                check = 1;
            }
            else
            {
                 
                // If count is less than K
                // return false
                if (count < K)
                {
                    return false;
                }
            }
            count = 0;
        }
        else
        {
             
            // Adding the count as the
            // number of zeroes increase
            // between set bits
            count++;
        }
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 5;
    int K = 1;
 
    if (CheckBits(N, K))
    {
        System.out.print("YES");
    }
    else
    {
        System.out.print("NO");
    }
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to check if all the set
# bits of the binary representation
# of N are at least K places away.
def CheckBits(N, K):
     
    # Initialize check and count
    # with 0
    check = 0
    count = 0
 
    for i in range(31, -1, -1):
 
        # The i-th bit is a set bit
        if ((1 << i) & N):
 
            # This is the first set bit so,
            # start calculating all the
            # distances between consecutive
            # bits from here
            if (check == 0):
                check = 1
 
            else:
 
                # If count is less than K
                # return false
                if (count < K):
                    return False
 
            count = 0
 
        else:
 
            # Adding the count as the
            # number of zeroes increase
            # between set bits
            count += 1
 
    return True
 
# Driver code
if __name__ == "__main__":
     
    N = 5
    K = 1
 
    if (CheckBits(N, K)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by chitranayal


Javascript


输出:
YES