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;
}