Climbing Worm Problem - 1049

Text Reverse
Time Limit: 1000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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

问题链接
Problem - 1049

问题简述:
一只虫子掉进了深n英尺的井里,每分钟爬行u英尺,每爬行一分钟就要休息一分钟,休息时每分钟掉落d英尺,问虫子经过多少分钟才能爬出井。

问题分析:
做一个简单的循环记录分钟数即可。

程序说明:
首先先让虫子爬行u米,然后再进行循环运算。

#include 
#include 
using namespace std;

int main()
{
	int n,d,u,i;//深度为n,每分钟爬行u米,休息时每分钟滑下d米; 
	while(cin>>n>>u>>d)
	{
		if(n==0)
		break;
		n-=u;
		for(i=1;n>0;i+=2)
		{
			n+=d;
			n-=u;
		}
	    cout<

你可能感兴趣的:(ACM训练题)