位运算才是经典
#include<iostream> #include<algorithm> #include<string> #include<set> #include<deque> #include<map> #include<stack> #include<vector> using namespace std; int main() { int n; cin >> n; int count; for (count = 0; n; n &= n - 1) { count++; } cout << count << endl; system("pause"); return 0; }