2252: Pick Balls

2252: Pick Balls


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K373158Standard
Sherry is an excellent student of Jilin University. Today her teacher gives her a bag which is filled with M balls. She wants to pick all of them out. To make it interesting, she makes a rule as following:

Assume that the number of balls she picks last time is N,then she can only pick N, N/2 or N*2 balls this time. She can pick only one ball at the beginning. Of course, when N is equal to 1, she can not pick 0 ball this time.

Certainly she can always do that successfully no matter what value M is.But, to make it more difficult, she wants to use the fewest times to finish the task.

Could you help her? Thank you.:)

Input

There are T (T < 50000)lines in the input file.Each line there is a positive integer M (1 <= M <= 2 31-1) which indicates the total number of balls.

The end of the input will be indicated by an integer value of zero.

Output

For each integer in the input, you should print a integer in a single line which is the minimum times Sherry needs to pick these exactly M balls out.

Sample Input

2
3
8
0

Sample Output

2
2
4

hints:

2: 1 1
3: 1 2
8: 1 1 2 4
#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n&&n)
    {
        int cnt=0,temp=1,sum=0;
        while(sum<n)
        {
            cnt++;
            sum+=temp;
            temp*=2;
        }
        temp/=2;
        sum-=temp;
        sum=n-sum;
        cnt--;
        while(sum)
        {
            cnt+=sum/temp;
            sum%=temp;
            temp/=2;
        }
        cout<<cnt<<endl;
    }
    return 0;
}
Problem Statement There is an empty bag. You are given Q queries. Process these queries in order and output the answer to each type- 2 query. Each query is of one of the following types. Type 1: Given as input in the format 1 x. Put a ball with the integer x written on it into the bag. Type 2: Given as input in the format 2. Pick out one ball with the minimum integer written on it from the balls in the bag, and report that integer as the answer. This query is not given when the bag contains no balls. Constraints 2≤Q≤100 In a type- 1 query, 1≤x≤100. When a type- 2 query is given, the bag is not empty. At least one type- 2 query is given. All input values are integers. Input The input is given from Standard Input in the following format: Q query 1 ​ query 2 ​ … query Q ​ Here, query i ​ is the i-th query and is given in one of the following formats: 1 x 2 Output Let q be the number of type- 2 queries, and output q lines. The i-th line should contain the answer to the i-th type- 2 query. Sample Input 1 Copy 5 1 6 1 7 2 1 1 2 Sample Output 1 Copy 6 1 Initially, the bag contains no balls. The 1st query puts a ball with 6 written on it into the bag. The 2nd query puts a ball with 7 written on it into the bag. In the 3rd query, the bag contains a ball with 6 written on it and a ball with 7 written on it, so you pick out the ball with 6 written on it. The answer to this query is 6. The 4th query puts a ball with 1 written on it into the bag. In the 5th query, the bag contains a ball with 1 written on it and a ball with 7 written on it, so you pick out the ball with 1 written on it. The answer to this query is 1. Sample Input 2 Copy 8 1 5 1 1 1 1 1 9 2 2 1 2 2 Sample Output 2 Copy 1 1 2 The bag may contain multiple balls with the same integer. c++
最新发布
08-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值