给定一个十进制非负整数N,求其对应二进制数中1的个数。
输入格式:一行一个非负整数N(N<1e9)。
输出格式:一行一个整数表示N对应二进制中1的个数.
输入样例:
100
输出样例:
3
题目很简单,首先,我们应将输入的十进制整数转换为一个二进制数,其次,对其取模,求出其中一的个数。废话不多说,上代码!
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define N 10010
int a[N];
int main()
{
int n, ans = 0;
cin >> n;
for (int i = n; i >= 1; i--)
{
ans += n % 2;
n /= 2;
}
cout << ans;
return 0;
}
之后,这道题目就过啦!