Climbing Worm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12570 Accepted Submission(s): 8491
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
Source
题目大意:
水题一道 , 说看注释吧
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
//题目大意 就是一共有N步 虫值每分钟走u 步 然后休息一分钟后d步
int n , d , u;
while(scanf("%d%d%d", &n, &u, &d), n)
{
int len = 0;//用来记录虫子走的路程
int sum = 0 ;//用来记录时间
while(len < n)
{
sum += 1;
len += u;
if(len >= n)
{//大于等于那么退出
break; //跳出循环的条件
}
len -= d; //休息一分钟
sum += 1;
}
printf("%d\n", sum);
}
}
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com