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.

24 lines
450 B

#include <bits/stdc++.h>
using namespace std;
//计算数字n中有多少个二进制1
int hammingWeight(int n) {
if (n == 0) return 0;
int cnt = 0;
while (n) {
n &= (n - 1);
cnt++;
}
return cnt;
}
int main() {
int n = 10, m = 4;
//暴力算
int cnt = 0;
for (int i = 0; i < (1 << n); i++)
if (hammingWeight(i) == m) cnt++;
cout << cnt << endl;
return 0;
}