【题目描述】
给定一个十进制非负整数 N,求其对应 2 进制数中 1 的个数。
【输入】
输入包含一行,包含一个非负整数 N。(N≤109)
【输出】
输出一行,包含一个整数,表示 N 的 2 进制表示中 1 的个数。
【输入样例】
100
【输出样例】
3
源代码:
#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #define LL long long using namespace std; int lowbit(int x) { return x&(-x); } int main() { int n,k=0; cin>>n; while(n) {n-=lowbit(n); k++;} cout<<k; return 0; }