You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
int main ( ) {
int n = 5 ; //集合元素个数上限
int U = 1 < < n ; //U-1即为全集 ,比如 1<<5 就是 2的5次方, 就是32, U=32。而U-1=31, 就是表示 1 1 1 1 1
// 1~ 31的所有数字二进制表现形式
//方法1: 固定1, 右移n大法
for ( int i = 1 ; i < U ; i + + ) {
for ( int j = 4 ; j > = 0 ; j - - )
cout < < ( ( i > > j ) & 1 ) ;
printf ( " \n " ) ;
}
printf ( " ***************************************************** \n " ) ;
//方法2: 固定n, 左移1大法
for ( int i = 1 ; i < U ; i + + ) {
for ( int j = 4 ; j > = 0 ; j - - ) //这里写7~0是为了少一些前导0, 看着方便,注意从大到小,才能正确显示
if ( i & ( 1 < < j ) ) cout < < 1 ; else cout < < 0 ;
cout < < endl ;
}
printf ( " ***************************************************** \n " ) ;
//方法3: bitset大法
for ( int i = 1 ; i < U ; i + + ) {
//方法1:
bitset < 5 > a ( i ) ;
cout < < a . to_string ( ) < < endl ;
}
printf ( " ***************************************************** \n " ) ;
for ( int i = 5 - 1 ; i > = 0 ; i - - ) //2的4次方, 2的3次方, ... 2的0次方, 共5位
cout < < ( 1 < < i ) < < " " ;
return 0 ;
}