24 lines
450 B
C++
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;
|
|
} |