原文:
给定一个数字 n,打印所有的数字,这些数字是 n 的二进制表示的按位“与”集合。按位“与”集合 n 是所有可能的小于或等于 n 的数字 x,使得对于某个数字 i,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.
python 3
# 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.
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
输出:
0 1 8 9
时间复杂度: o(n)
高效pg电子试玩链接的解决方案:高效的pg电子试玩链接的解决方案是使用按位运算符来查找子集。我们可以只迭代按位子集,而不是迭代每个 i。向后迭代 i=(i-1) & n 给我们每个按位子集,其中我从 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.
python 3
# 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.
服务器端编程语言(professional hypertext preprocessor 的缩写)
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 的按位子集数
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处