您的位置:首页 > 编程语言 > Java开发

ZOJ - 3498 Javabeans

2017-02-09 21:05 465 查看
Javabeans
Time Limit: 2 Seconds      Memory Limit: 65536 KB
Javabeans are delicious. Javaman likes to eat javabeans very much.
Javaman has n boxes of javabeans. There are exactly i javabeans in the i-th box (i = 1, 2, 3,...n). Everyday Javaman chooses an
integer x. He also chooses several boxes where the numbers of javabeans are all at least x. Then he eats x javabeans in each box he has just chosen. Javaman wants to eat all the javabeans up as soon as possible. So how many
days it costs for him to eat all the javabeans?
Input
There are multiple test cases. The first line of input is an integer T ≈ 100 indicating the number of test cases.
Each test case is a line of a positive integer 0 < n < 231.
Output
For each test case output the result in a single line.
Sample Input

4
1
2
3
4


Sample Output

1
2
2
3


Author: CAO, Peng
Contest: The 8th Zhejiang Provincial Collegiate Programming Contest

也是水题,但是我理解了好久,一直搞不懂什么意思,水题不代表就可以不去理解就做啦

题意:有n个盒子,盒子中javabean的数目分别为1-n,给你盒子的数目,如果你决定第一天吃n个豆豆,在盒子中豆豆数目>=n个的每个盒子中吃掉n个豆豆,以此类推,问最快几天能吃掉所有盒子中的所有豆豆。

规律:1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5...

比如五个 1 2 3 4 5

第一次吃5/2+1=3  结果:1 2 0 1 2

第二次吃3/2+1=2  结果:1 0 0 1 0

第三次吃1/2+1=1  结果:0 0 0 0 0

即每次在剩下的盒子中找到有最大豆子的盒子,取这个盒子豆子数量的一半

取得次数即为所求

#include <iostream>

using namespace std;

int main()
{
int T,ans;
cin>>T;
int n;
while(T--)
{
ans=0;
cin>>n;
while(n)
{
ans++;
n/=2;
}
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM zoj 水题