**
HDU-1049 Climbing Worm
**
问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1049
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
问题描述:
有一只一英寸长的毛毛虫在离井口n英寸的井底,它每分钟能爬u英寸,但它每爬完一分钟必须休息一分钟,休息过程中会下滑d英寸。问这只毛毛虫要花多长时间爬到井口。
问题分析:
按照题目的要求做就好了,建立变量保存时间,虫子每爬一次加一分钟,休息时也加一分钟,爬完时跳出循环。
程序如下:
#include<iostream>
using namespace std;
int main()
{
int i;//保存爬时
int a;//井到井底的距离
int b;//虫子每分钟爬的路程
int c;//虫子休息时滑落的路程
int s;//保存虫子爬的路程
while (1)
{
cin >> a >> b >> c;
s = 0;
for (i = 0; s < a; i++)
{
s += b;
i++;//每爬一次加一分钟
if (s >= a)
{
break;//如果虫子在这一分钟爬出井的话,终止循环
}
s -= c;//休息滑落时也要花一分钟
}
if (a==0)break;
cout << i << endl;
}
}