📌  相关文章
📜  用正好两个设置位打印前n个数字

📅  最后修改于: 2021-04-26 17:55:23             🧑  作者: Mango

给定数字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开始一个个地考虑所有正整数。对于每个数字,检查它是否恰好有两个置位比特。如果一个数字恰好有两个设置位,请打印该数字并递增计数。
一个有效的解决方案是直接生成此类数字。如果我们清楚地观察到这些数字,则可以将它们重写为pow(2,1)+ pow(2,0),pow(2,2)+ pow(2,0),pow(2,2)+ pow (2,1),pow(2,3)+ pow(2,0),pow(2,3)+ pow(2,1),pow(2,3)+ pow(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


Python3
# 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.


PHP
 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
?>


Javascript


输出 :

3 5 6 9