Files
python/TangDou/LuoGuBook/P1036_Prepare_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

35 lines
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}