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

24 lines
450 B
C++

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