atcoder ABC 354-A题详解

atcoder ABC 354-A题详解

Problem Statement

Takahashi is growing a plant. Its height at the time of germination is 0cm. Considering the day of germination as day 0, its height increases by 2icm day i’s night (0≤i).

Takahashi’s height is Hcm.

Every morning, Takahashi measures his height against this plant. Find the first day such that the plant’s height is strictly greater than Takahashi’s height in the morning.

Constraints

1≤H≤109
All input values are integers.

Input

The input is given from Standard Input in the following format:

H

Output

Print an integer representing the first day such that the plant’s height is greater than Takahashi’s height in the morning.

Sample Input 1

54

Sample Output 1

6
The plant’s height in the mornings of days 1,2,3,4,5,6 will be 1cm,3cm,7cm,15cm,31cm,63cm, respectively. The plant becomes taller than Takahashi in the morning day 6, so print 6.

Sample Input 2

7

Sample Output 2

4
The plant’s height will be 7cm in the morning of day 3 and 15cm in the morning day 4. The plant becomes taller than Takahashi in the morning of day 4, so print 4. Note that, in the morning of day 3, the plant is as tall as Takahashi, but not taller.

Sample Input 3

262144

Sample Output 3

19

思路分析:

求植物长几天比高桥的身高高,植物一天长2的i次方,a是用来存储植物当前天数的高度,当a>h时,说明植物的高度比高桥的身高高,但是i是从1开始的,起始应为0,所以b要➕1;当a=h时,说明植物的高度与高桥的身高相等,需要再过1天植物的高度才比高桥高,所以b要➕2。

code:

#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
int h;//高桥的身高
int a=1;
int b;
int main(int argc, const char * argv[]) {
    cin>>h;
    for(int i=1;i<0X3f3f3f3f;i++){
        a+=pow(2,i);
        b=i;
        if(a>h){
            cout<<b+1;
            break;
        }
        if(a==h){
            cout<<b+2;
            break;
        }
    }
    return 0;
}
  • 20
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值