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.

35 lines
1.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 5; //集合元素个数上限
int U = 1 << n; //U-1即为全集 ,比如 1<<5 就是 2的5次方就是32U=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");
//方法3bitset大法
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;
}