落絮飞雁的个人网站
HDOJ1049:Climbing Worm——基础题
HDOJ1049: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


像是小学算术题。没什么好说的,一次过。
放上代码:

#include
int main()
{
	int n, u, d, route, t;
	while (scanf("%d%d%d", &n, &u, &d) && n)
	{
		t = 0;
		route = n;
		while (1)
		{
			route -= u;
			t++;
			if (route <= 0)
				break;
			route += d;
			t++;
		}
		printf("%dn", t);
		n = u = d = 0;
	}
	return 0;
}
原文标题:HDOJ1049:Climbing Worm——基础题|落絮飞雁的个人网站
授权协议:创作共用 署名-非商业性使用 2.5 中国大陆
除注明外,本站文章均为原创;转载时请保留上述链接。
没有标签
首页      ACM      HDOJ      HDOJ1049:Climbing Worm——基础题

发表评论

textsms
account_circle
email

落絮飞雁的个人网站

HDOJ1049: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 …
扫描二维码继续阅读
2015-03-13