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.

51 lines
1.5 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
/*
11n1n-1
13=(1101)b31
111000,0100,0001
211100,1001,0101
6
*/
// 输出十进制对应的二进制数(模板)
void print(int n) {
printf("%2d:", n);
// 输出4个数位的二进制数
for (int i = 3; i >= 0; i--) // 由高到低位
printf("%d", n >> i & 1);
puts("");
}
/*
n
1
(1) i=n-1:1
(2) for (int i = n - 1; i; i--),
(3) if ((i & n) == i) 1
*/
void sub1(int n) {
for (int i = n - 1; i; i--)
if ((i & n) == i) print(i);
}
/*
2
*/
void sub2(int n) {
for (int i = n - 1; i; i = (i - 1) & n)
print(i);
}
int main() {
int st = 13; // 1101
sub1(st);
printf("==================================\n");
sub2(st);
return 0;
}