时间限制:20000ms
单点时限:1000ms
内存限制:256MB
描述
给定一个数 n,你可以进行若干次操作,每次操作可以翻转 n 的二进制表示下的某一位,即将 0 变成 1,1 变成 0
现在小 Hi 想知道,至少需要多少次操作,才能将 n 变成 n-1
输入
一个正整数 n
1 ≤ n ≤ 109
输出
输出最少的操作次数
- 样例输入
-
10
- 样例输出
-
2
1 // 2018-07-29 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #define ll long long 7 8 using namespace std; 9 10 int main() 11 { 12 ll n; 13 while(cin>>n){ 14 ll ans = n^(n-1); 15 int cnt = 0; 16 while(ans){ 17 if(ans&1LL){ 18 cnt++; 19 } 20 ans>>=1; 21 } 22 cout<<cnt<<endl; 23 } 24 25 26 return 0; 27 }