'commit'
This commit is contained in:
35
TangDou/LuoGuBook/P1036_Prepare_1.cpp
Normal file
35
TangDou/LuoGuBook/P1036_Prepare_1.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user