codeforces #320 div 2A - Raising Bacteria (位运算)

x的二进制表示中1的个数即为答案.

原因是,每天晚上糖果数量翻倍,相当于左移1位,这时候二进制表示中1的数量不变

也就是说,二进制表示中的所有的1,一定都是添加进去的

而且也只有二进制表示中的1是添加进去的

所以二进制表示中1的数量,就是添加的糖果数.

/*************************************************************************
	> File Name: code/cf/#320/A.cpp
	> Author: 111qqz
	> Email: rkz2013@126.com 
	> Created Time: 2015年09月18日 星期五 23时18分34秒
 ************************************************************************/
 1#include<iostream>
 2#include<iomanip>
 3#include<cstdio>
 4#include<algorithm>
 5#include<cmath>
 6#include<cstring>
 7#include<string>
 8#include<map>
 9#include<set>
10#include<queue>
11#include<vector>
12#include<stack>
13#include<cctype>
14#define y1 hust111qqz
15#define yn hez111qqz
16#define j1 cute111qqz
17#define ms(a,x) memset(a,x,sizeof(a))
18#define lr dying111qqz
19using namespace std;
20#define For(i, n) for (int i=0;i<int(n);++i)  
21typedef long long LL;
22typedef double DB;
23const int inf = 0x3f3f3f3f;
24int main()
25{
26  #ifndef  ONLINE_JUDGE 
 1  #endif
 2    int x;
 3    cin>>x;
 4    int ans = 0 ;
 5    while (x)
 6    {
 7	if (x%2==1) ans++;
 8	x = x / 2;
 9    }
10    cout<<ans<<endl;
1 #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4	return 0;
5}