ZOJ3498 Javabeans(找規律)

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

題意:
    n個盒子,第i個盒子裏有i個javabean,每天選一個整數x,將至少含有x個javabean的盒子裏的javabean喫掉x個,問最少幾天喫完?

思路:
    每天選擇的x應爲這一天一個盒子中javabean的最大數,這樣每喫一次,最大個數就變爲昨天的一半,這樣天數的計算就可以用遞推得到了,n擴大2倍,天數就加1。

#include<stdio.h>
int s[32];
int main()
{
    int t,n,i;
    for(i=1; i<32; i++)
        s[i]=1<<i-1;
    while(scanf("%d",&t)!=EOF)
        while(t--)
        {
            scanf("%d",&n);
            for(i=31; i>=1; i--)
                if(n>=s[i])
                {
                    printf("%d\n",i);
                    break;
                }
        }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章