题解 | #查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
#include <iostream> #include <map> using namespace std; int main() { int data; vector<int> ram; while(cin >> data) { ram.push_back(data); } map<int ,int>Num; for (auto it : ram) { int n = it; while (n != 0) { int a = n % 2; Num[a] += 1; n = n / 2; } for (auto it : Num) { if (it.first == 1) { cout << it.second << endl; } } Num.clear(); } } // 64 位输出请用 printf("%lld")