...
如果num是奇数,那么它等于num/2的二进制表示中1的个数加1.
#include <iostream>
using namespace std;
int number_of_one(int num)
{
if (num < 2)
return num;
return number_of_one(num / 2) + num % 2;
}
int main()
{
int num;
cin >> num;
cout << number_of_one(num) << endl;
return 0;
}