📜  1到n位数字,二进制表示中没有连续的1

📅  最后修改于: 2021-05-25 09:01:34             🧑  作者: Mango

给定一个数字n,我们的任务是找到所有1到n个二进制数,且其二进制表示中没有连续的1。
例子:

Input: N = 4
Output: 1 2 4 5 8 9 10
These are numbers with 1 to 4
bits and no consecutive ones in
binary representation.

Input: n = 3
Output: 1 2 4 5

方法:

  1. 将有2 n个数字,位数从1到n。
  2. 遍历所有2 n个数字。对于每个数字,请检查其是否包含连续的设置位。为了进行检查,我们对当前数字i进行左移和对i进行左移。如果按位且包含非零位(或其值不为零),则给定数字包含连续的设置位。

下面是上述方法的实现:

C++
// Print all numbers upto n bits
// with no consecutive set bits.
#include
using namespace std;
 
void printNonConsecutive(int n)
{
    // Let us first compute
    // 2 raised to power n.
    int p = (1 << n);
 
    // loop 1 to n to check
    // all the numbers
    for (int i = 1; i < p; i++)
 
        // A number i doesn't contain
        // consecutive set bits if
        // bitwise and of i and left
        // shifted i do't contain a
        // commons set bit.
        if ((i & (i << 1)) == 0)
            cout << i << " ";
}
 
// Driver code
int main()
{
    int n = 3;
    printNonConsecutive(n);
    return 0;
}


Java
// Java Code to Print all numbers upto
// n bits with no consecutive set bits.
import java.util.*;
 
class GFG
{
    static void printNonConsecutive(int n)
        {
            // Let us first compute
            // 2 raised to power n.
            int p = (1 << n);
 
            // loop 1 to n to check
            // all the numbers
            for (int i = 1; i < p; i++)
 
            // A number i doesn't contain
            // consecutive set bits if
            // bitwise and of i and left
            // shifted i do't contain a
            // commons set bit.
            if ((i & (i << 1)) == 0)
                System.out.print(i + " ");
         
        }
 
// Driver code
public static void main(String[] args)
    {
        int n = 3;
        printNonConsecutive(n);
    }
}
 
// This code is contributed by Mr. Somesh Awasthi


Python3
# Python3 program to print all numbers upto
# n bits with no consecutive set bits.
 
def printNonConsecutive(n):
 
    # Let us first compute 
    # 2 raised to power n.
    p = (1 << n)
 
    # loop 1 to n to check
    # all the numbers
    for i in range(1, p):
 
        # A number i doesn't contain
        # consecutive set bits if
        # bitwise and of i and left
        # shifted i do't contain a
        # common set bit.
        if ((i & (i << 1)) == 0):
            print(i, end = " ")
 
# Driver code
n = 3
printNonConsecutive(n)
 
# This code is contributed by Anant Agarwal.


C#
// C# Code to Print all numbers upto
// n bits with no consecutive set bits.
using System;
 
class GFG
{
    static void printNonConsecutive(int n)
    {
        // Let us first compute
        // 2 raised to power n.
        int p = (1 << n);
 
        // loop 1 to n to check
        // all the numbers
        for (int i = 1; i < p; i++)
 
            // A number i doesn't contain
            // consecutive set bits if
            // bitwise and of i and left
            // shifted i do't contain a
            // commons set bit.
            if ((i & (i << 1)) == 0)
                Console.Write(i + " ");
         
    }
 
// Driver code
public static void Main()
    {
        int n = 3;
        printNonConsecutive(n);
    }
}
// This code is contributed by nitin mittal.


PHP


Javascript


输出
1 2 4 5 

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