📜  按位打印并设置数字N

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

给定数字N,请打印所有数字,这些数字是N的二进制表示的按位与集合。数字N的按位与集合是所有可能的数字x小于或等于N,使得对于某些情况,N&i等于x第一号

例子 :

Input : N = 5
Output : 0, 1, 4, 5 
Explanation: 0 & 5 = 0 
             1 & 5 = 1
             2 & 5 = 0
             3 & 5 = 1
             4 & 5 = 4
             5 & 5 = 5  
So we get 0, 1, 4 and 5 in the 
bitwise subsets of N.

Input : N = 9
Output : 0, 1, 8, 9 

简单方法:天真的方法是从0到N的所有数字进行迭代,并检查是否(N&i == i)。打印满足指定条件的数字。

下面是上述想法的实现:

C++
// CPP program to print all bitwise
// subsets of N (Naive approach)
#include 
using namespace std;
 
// function to find bitwise subsets
// Naive approach
void printSubsets(int n) {
  for (int i = 0; i <= n; i++)
    if ((n & i) == i)
      cout << i << " ";
}
 
// Driver Code
int main() {
   
  int n = 9;
  printSubsets(n);
  return 0;
}


Java
// JAVA program to print all bitwise
// subsets of N (Naive approach)
class GFG {
     
    // function to find bitwise subsets
    // Naive approach
    static void printSubsets(int n)
    {
         
        for (int i = 0; i <= n; i++)
            if ((n & i) == i)
                System.out.print(i + " ");
    }
     
    // Driver function
    public static void main(String[] args)
    {
        int n = 9;
         
        printSubsets(n);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to print all bitwise
# subsets of N (Naive approach)
def printSubsets(n):
     
    for i in range(n + 1):
         
        if ((n & i) == i):
            print(i ," ", end = "")
 
# Driver code
n = 9
printSubsets(n)
 
# This code is contributed by Anant Agarwal.


C#
// C# program to print all bitwise
// subsets of N (Naive approach)
using System;
 
class GFG {
     
    // function to find bitwise subsets
    // Naive approach
    static void printSubsets(int n)
    {
         
        for (int i = 0; i <= n; i++)
            if ((n & i) == i)
                Console.Write(i + " ");
    }
     
    // Driver function
    public static void Main()
    {
        int n = 9;
         
        printSubsets(n);
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP program to print all bitwise
// subsets of N (Efficient approach)
 
#include 
using namespace std;
 
// function to find bitwise subsets
// Efficient approach
void printSubsets(int n) {
   
  for (int i = n; i > 0; i = (i - 1) & n)
    cout << i << " ";
  cout << 0;
}
 
// Driver Code
int main() { 
  int n = 9; 
  printSubsets(n); 
  return 0;
}


Java
// Java program to print all bitwise
// subsets of N (Efficient approach)
 
class GFG
{
 
    // function to find bitwise
    // subsets Efficient approach
    static void printSubsets(int n)
    {
    for (int i = n; i > 0; i = (i - 1) & n)
 
        System.out.print(i + " ");
        System.out.print(" 0 ");
     
    }
 
// Driver Code
public static void main(String[] args)
{
    int n = 9;
    printSubsets(n);
}
}
 
// This code is contributed by ajit.


Python3
# Python 3 program to
# print all bitwise
# subsets of N
# (Efficient approach)
 
# function to find
# bitwise subsets
# Efficient approach
def printSubsets(n):
    i=n
    while(i != 0):
        print(i,end=" ")
        i=(i - 1) & n
    print("0")
 
# Driver Code
n = 9
printSubsets(n)
 
# This code is contributed by
# Smith Dinesh Semwal


C#
// C# program to print all bitwise
// subsets of N (Efficient approach)
using System;
 
public class GFG {
 
    // function to find bitwise subsets
    // Efficient approach
    static void printSubsets(int n) {
     
    for (int i = n; i > 0; i = (i - 1) & n)
        Console.Write(i +" ");
        Console.WriteLine("0");
    }
     
    // Driver Code
    static public void Main () {
         
        int n = 9;
         
        printSubsets(n);
    }
}
 
// This code is contributed by vt_m.


PHP
 0;
         $i = ($i - 1) & $n)
          
        echo $i." ";
    echo "0";
}
 
// Driver Code
$n = 9;
printSubsets($n);
 
// This code is contributed by mits
?>


Javascript


输出 :

0 1 8 9

时间复杂度: O(N)

高效的解决方案:高效的解决方案是使用按位运算运算符查找子集。除了对每个i进行迭代,我们还可以仅对按位子集进行迭代。向后迭代i =(i-1)&n给我们每个按位子集,其中i从n开始并以1结尾。

下面是上述想法的实现:

C++

// CPP program to print all bitwise
// subsets of N (Efficient approach)
 
#include 
using namespace std;
 
// function to find bitwise subsets
// Efficient approach
void printSubsets(int n) {
   
  for (int i = n; i > 0; i = (i - 1) & n)
    cout << i << " ";
  cout << 0;
}
 
// Driver Code
int main() { 
  int n = 9; 
  printSubsets(n); 
  return 0;
}

Java

// Java program to print all bitwise
// subsets of N (Efficient approach)
 
class GFG
{
 
    // function to find bitwise
    // subsets Efficient approach
    static void printSubsets(int n)
    {
    for (int i = n; i > 0; i = (i - 1) & n)
 
        System.out.print(i + " ");
        System.out.print(" 0 ");
     
    }
 
// Driver Code
public static void main(String[] args)
{
    int n = 9;
    printSubsets(n);
}
}
 
// This code is contributed by ajit.

Python3

# Python 3 program to
# print all bitwise
# subsets of N
# (Efficient approach)
 
# function to find
# bitwise subsets
# Efficient approach
def printSubsets(n):
    i=n
    while(i != 0):
        print(i,end=" ")
        i=(i - 1) & n
    print("0")
 
# Driver Code
n = 9
printSubsets(n)
 
# This code is contributed by
# Smith Dinesh Semwal

C#

// C# program to print all bitwise
// subsets of N (Efficient approach)
using System;
 
public class GFG {
 
    // function to find bitwise subsets
    // Efficient approach
    static void printSubsets(int n) {
     
    for (int i = n; i > 0; i = (i - 1) & n)
        Console.Write(i +" ");
        Console.WriteLine("0");
    }
     
    // Driver Code
    static public void Main () {
         
        int n = 9;
         
        printSubsets(n);
    }
}
 
// This code is contributed by vt_m.

的PHP

 0;
         $i = ($i - 1) & $n)
          
        echo $i." ";
    echo "0";
}
 
// Driver Code
$n = 9;
printSubsets($n);
 
// This code is contributed by mits
?>

Java脚本


输出 :

9 8 1 0

时间复杂度:O(K),其中K是N的按位子集的数量。