1.2.1Climbing Worm(附翻译)

探讨一个关于蠕虫从井底爬升的问题,通过编程解决蠕虫在爬升过程中的复杂状态变化,包括上升与下滑的过程,并给出具体的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5750 AcceptedSubmission(s): 3389

Problem Description

An inch worm is at the bottom of a well ninches deep. It has enough energy to climb u inches every minute, but then hasto rest a minute before climbing again. During the rest, it slips down dinches. The process of climbing and resting then repeats. How long before theworm climbs out of the well? We'll always count a portion of a minute as awhole minute and if the worm just reaches the top of the well at the end of itsclimbing, we'll assume the worm makes it out.

一英寸蠕虫在深井的底部。 它有足够的能量每分钟爬上u英寸,但在再次爬升之前需要休息一分钟。 在休息期间,它会滑下餐桌。 然后攀爬和休息的过程重复。 在蠕虫爬出井之前多久? 我们总是将一分钟的一小部分计为一分钟,如果蠕虫在爬坡结束时刚刚到达井顶,我们就会认为蠕虫将其排除。

Input

There will be multiple problem instances.Each line will contain 3 positive integers n, u and d. These give the valuesmentioned in the paragraph above. Furthermore, you may assume d < u and n< 100. A value of n = 0 indicates end of output.

将会有多个问题实例。每行将包含3个正整数n,u和d。 这些给出了上面段落中提到的值。 此外,您可以假设d <u和n <100。n = 0的值表示输出结束。

Output

Each input instance should generate asingle integer on a line, indicating the number of minutes it takes for theworm to climb out of the well.

每个输入实例应该在一行上生成一个整数,表示蠕虫爬出井所需的分钟数。


10 2 1
20 3 1
0 0 0

Sample Output

17
19

本题解题难度在于在攀爬过程中还不断下滑,而且需要额外注意并不是每次爬都有一次下滑,例如第一个实例当爬到8的位置再往上爬2就直接出去了,a为井深,b是速度,c是下滑距离,采用的方法就是以a-b的距离除以b-c的速度因为是自动取整,所以要是有余数就+1,这算的是爬的次数,因为每次爬一上一滑两秒所以*2,后面+1是因为前面路程-b了(写晕了,也不知道咋解释,手动捂脸)

c++

#include<iostream>
using namespace std;
int main(){
	int a,b,c,d;
	while(cin>>a>>b>>c&&a!=0){
		if((a-b)%(b-c)!=0){
			d=((a-b)/(b-c)+1)*2+1;
		}else{
			d=((a-b)/(b-c))*2+1;
		}
		cout<<d<<endl;
	}
	return 0;
}

运行截图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值