原文:

给定一个数字 n,打印前 n 个正整数,在其二进制表示中正好有两个设置位。 例:

input: n = 3
output: 3 5 6
the first 3 numbers with two set bits are 3 (0011),
5 (0101) and 6 (0110)
input: n = 5
output: 3 5 6 9 10 12

一个简单解法就是从 1 开始逐个考虑所有正整数。对于每个数字,检查它是否正好有两组位。如果一个数字正好有两个设定位,打印出来并增加这些数字的计数。 一个高效的pg电子试玩链接的解决方案就是直接生成这样的数字。如果我们清楚地观察这些数字,我们可以把它们改写为下面给定的幂(2,1) 幂(2,0),幂(2,2) 幂(2,0),幂(2,2) 幂(2,1),幂(2,3) 幂(2,0),幂(2,3) 幂(2,1),幂(2,3) 幂(2,2),…… 所有的数字都可以按照两组位中较高的一组按递增的顺序产生。这个想法是一个接一个地固定两个比特中较高的一个。对于当前较高的设置位,考虑所有较低的位并打印形成的数字。

c

// c   program to print first n numbers
// with exactly two set bits
#include 
using namespace std;
// prints first n numbers with two set bits
void printtwosetbitnums(int n)
{
    // initialize higher of two sets bits
    int x = 1;
    // keep reducing n for every number
    // with two set bits.
    while (n > 0)
    {
        // consider all lower set bits for
        // current higher set bit
        int y = 0;
        while (y < x)
        {
            // print current number
            cout << (1 << x)   (1 << y) << " ";
            // if we have found n numbers
            n--;
            if (n == 0)
                return;
            // consider next lower bit for current
            // higher bit.
            y  ;
        }
        // increment higher set bit
        x  ;
    }
}
// driver code
int main()
{
    printtwosetbitnums(4);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print first n numbers
// with exactly two set bits
import java.io.*;
class gfg
{
    // function to print first n numbers with two set bits
    static void printtwosetbitnums(int n)
    {
        // initialize higher of two sets bits
        int x = 1;
        // keep reducing n for every number
        // with two set bits
        while (n > 0)
        {
            // consider all lower set bits for
            // current higher set bit
            int y = 0;
            while (y < x)
            {
                // print current number
                system.out.print(((1 << x)   (1 << y))  " ");
                // if we have found n numbers
                n--;
                if (n == 0)
                    return;
                // consider next lower bit for current
                // higher bit.
                y  ;
            }
            // increment higher set bit
            x  ;
        }
    }
    // driver program
    public static void main (string[] args)
    {
        int n = 4;
        printtwosetbitnums(n);
    }
}
// this code is contributed by pramod kumar

python 3

# python3 program to print first n
# numbers with exactly two set bits
# prints first n numbers
# with two set bits
def printtwosetbitnums(n) :
    # initialize higher of
    # two sets bits
    x = 1
    # keep reducing n for every
    # number with two set bits.
    while (n > 0) :
        # consider all lower set bits
        # for current higher set bit
        y = 0
        while (y < x) :
            # print current number
            print((1 << x)   (1 << y),
                          end = " " )
            # if we have found n numbers
            n -= 1
            if (n == 0) :
                return
            # consider next lower bit
            # for current higher bit.
            y  = 1
        # increment higher set bit
        x  = 1
# driver code
printtwosetbitnums(4)
# this code is contributed
# by smitha

c

// c# program to print first n numbers
// with exactly two set bits
using system;
class gfg
    {
    // function to print first n
    // numbers with two set bits
    static void printtwosetbitnums(int n)
    {
        // initialize higher of
        // two sets bits
        int x = 1;
        // keep reducing n for every
        // number with two set bits
        while (n > 0)
        {
            // consider all lower set bits
            // for current higher set bit
            int y = 0;
            while (y < x)
            {
                // print current number
                console.write(((1 << x)  
                                (1 << y))  " ");
                // if we have found n numbers
                n--;
                if (n == 0)
                    return;
                // consider next lower bit
                // for current higher bit.
                y  ;
            }
            // increment higher set bit
            x  ;
        }
    }
    // driver program
    public static void main()
    {
        int n = 4;
        printtwosetbitnums(n);
    }
}
// this code is contributed by anant agarwal.

服务器端编程语言(professional hypertext preprocessor 的缩写)

 0)
    {
        // consider all lower set
        // bits for current higher
        // set bit
        $y = 0;
        while ($y < $x)
        {
            // print current number
            echo (1 << $x)   (1 << $y), " ";
            // if we have found n numbers
            $n--;
            if ($n == 0)
                return;
            // consider next lower
            // bit for current
            // higher bit.
            $y  ;
        }
        // increment higher set bit
        $x  ;
    }
}
// driver code
printtwosetbitnums(4);
// this code is contributed by ajit
?>

java 描述语言


输出:

3 5 6 9

时间复杂度: o(n)

辅助空间: o(1) 本文由巴拉思·雷迪·阿帕迪供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。