Climbing Worm

Problem Description
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.

Input
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.

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.

Sample Input
10 2 1
20 3 1
0 0 0

Sample Output
17
19

問題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1049
問題簡述:輸入三個(gè)數(shù),分別是總路程、每分鐘前進(jìn)路程、每分鐘下滑距離,
前進(jìn)一分鐘后下滑一分鐘,求要多少時(shí)間到達(dá)。
問題分析:用循環(huán)判斷到達(dá)是為幾分鐘。
程序說明:先判斷是否能到達(dá),再用for循環(huán)求幾分鐘時(shí)滿足條件。
AC通過的C++程序如下:

include<iostream>

using namespace std;
int main()
{
int n, u, d,s=0;
while (cin >> n >> u >> d)
{
if (u == 0 || u < d)
{
break;
}
else {
int work = 0;
for(s = n / u;work <n;)
{
s++;
work = u (s/2) - (d((s/2) - 1));
}
}
s--;
cout << s << endl;
s = 0;
}
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容