题干
An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We’ll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we’ll assume the worm makes it out.
大致翻译
一只英寸的蠕虫在一个n英寸深的井底。它有足够的能量每分钟爬升u英寸,但在再次爬升之前必须休息1分钟。在休息期间,它滑落了d英寸。然后重复爬升和休息的过程。在蠕虫爬出井口之前有多长时间?我们总是把一分钟的一部分算作整整一分钟,如果蠕虫在爬行结束时刚刚到达井口,我们就认为蠕虫已经爬出来了。
输入描述
There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end of output.
输出描述
Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the well.
样例
输入
10 2 1
20 3 1
0 0 0
输出
17
19
题解
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, u, d;
cin >> n >> u >> d;
while(n + u + d)
{
int res = 0;
if(u > n)//单次爬行距离超过总高度一次就可以出去
{
cout << 1 << endl;
continue;
}
else
{
n -= u;//最后一次单独计算
res = n/(u-d)*2;//将休息和爬行算在一次操作,单次操作用时两分钟
if(n%(u-d))
{
res += 2;//结果向上取整
}
res += 1;//加上最后一次的时间
cout << res << endl;
}
cin >> n >> u >> d;
}
return 0;
}