第二期训练题第二题

Climbing Worm

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23467 Accepted Submission(s): 16060

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)

问题简述:
一英寸的虫子在井的底部n英寸深。它有足够的能量每分钟爬u英寸,但在再次爬之前必须休息一分钟。在剩下的时间里,它下降了d英寸。攀爬和休息的过程会重复。虫子要多久才能爬出井?我们总是把一分钟的一部分当作一分钟来计算,如果虫子在爬完井的最后爬到了井口,我们就假定虫子爬出来了。

问题分析:
先让虫子先爬一分钟后,之后没两分钟虫子都会往上爬(u-d)英寸,然后计算虫子在爬一分钟后还需要爬多少个(u-d)英寸,每多爬一次时间就加2分钟,因为把一分钟的一部分当作一分钟来计算所以最后不满(u-d)也要加2,最后结果还要加上开始爬的一分钟,最后输出结果。

程序说明:
程序把爬完一分钟后之后还要爬多少次用((n-u)%(u-d))计算,而结果分为两种,一种是等于0,最后的结果为((n-u)%(u-d))*2+1,另一种为不等于0,结果为(((n-u)%(u-d))+1)*2+1,最后输出结果。

AC通过的C语言程序如下:

#include <iostream>
using namespace std;
int main()
{
	int n, u, d;
	while (cin >> n >> u >> d )
	{
		int k = 0;
		if (n==0&&u==0&&d==0)
			break;
		if ((n-u)%(u-d)==0)
		{
			k = 2 * (n - u) / (u - d);
			k++;
		}
		else
		{
			k = 2 * ((n - u)/(u - d) + 1) + 1;
		}
		cout << k << endl;
	}
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值