求32位整数含1的个数
#include<iostream> #include<string> #include<string.h> #include<stack> #include<queue> #include<map> #include<set> #include<algorithm> #include<memory> #include<vector> #define ll long long using namespace std; const int N = 100050; string s; int find1(ll n) { int x = 1, cnt = 0; while (x) {//最多循环32次,循环结束 if (x&n) { cnt++; } x = x << 1; } return cnt; } int find2(ll n) { int cnt = 0; while (n != 0) { n &= (n - 1); cnt++; } return cnt; } int main() { int n; cin >> n; cout << find2(n) << endl; system("pause"); return 0; }
等风起的那一天,我已准备好一切