2017年上海金马五校程序设计竞赛(网上资格赛)J : Raising Bacteria
2017-05-29 14:46
435 查看
Problem J : Raising Bacteria
From: DHUOJ, 2016060702Submit (Out of Contest)
Time Limit: 1 s
Description
You are a lover of bacteria so you want to raise some bacteria in a box. Initially, the box is empty. Every morning, you can put any number of bacteria into the box. Every night every bacterium in the box will split into two bacteria. You hope to see exactly xx bacteriain the box at some moment. What is the minimum number of bacteria you need to put into the box across those days?
Input
There are several test cases. Each case contains only one integer xx (1≤x≤109)(1≤x≤109) aline.
Output
For each case, output the only line containing one integer: the answer.Sample Input
5 8
Sample Output
2 1
Author: handoku
解题思路:题目是说有一个人他要在某个时刻看到x个细菌,首先细菌早上放一个,到晚上会繁殖成两个,问开始至少要放多少个细菌?
首先你要知道细菌每次都是2倍2倍分裂的,假如他看到的是2的n次方个(n取1,2,3........)那他最少放一个。。。
联系二进制数中的1的个数,他看到的细菌个数换成二进制就是要求的最小的放入的数目;
我的代码:
#include<iostream> using namespace std; int bin(int n) { int sum1=0; while(n) { if(n%2==1) sum1++; n=n/2; } return sum1; } int main() { int x; while(cin>>x) { cout<<bin(x)<<endl; } }
相关文章推荐
- 2017年上海金马五校程序设计竞赛(网上资格赛) B Coach(并查集)
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛(网上资格赛)H : DHU Club Festival
- 2017年上海金马五校程序设计竞赛(网上资格赛) Problem H : DHU Club Festival 贪心
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem A : Corn's new language
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem A : Corn's new language
- 2017年上海金马五校程序设计竞赛(网上资格赛) Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem H : DHU Club Festival
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem H : DHU Club Festival
- 2017年上海金马五校程序设计竞赛(网上资格赛) A : Corn's new language(括号匹配+求深度)
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem A : Corn's new language
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛)部分题目题解
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem E : A No-story Kingdom
- 2017年上海金马五校程序设计竞赛:Problem B : Sailing
- 2017年上海金马五校程序设计竞赛 A : STEED Cards 暴力
- 2017年上海金马五校程序设计竞赛 I : Frog's Jumping 找规律
- 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛—A