The Snail
| The Snail |
A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each successive day the snail climbs 10%
3 = 0.3 feet less than it did the previous day. (The distance lost to fatigue is always 10% of the first day's climbing distance.) On what day does the snail leave the well, i.e., what is the first day during which the snail's height exceeds 6 feet? (A day consists of a period of sunlight followed by a period of darkness.) As you can see from the following table, the snail leaves the well during the third day.
| Day | Initial Height | Distance Climbed | Height After Climbing | Height After Sliding |
| 1 | 0' | 3' | 3' | 2' |
| 2 | 2' | 2.7' | 4.7' | 3.7' |
| 3 | 3.7' | 2.4' | 6.1' | - |
Your job is to solve this problem in general. Depending on the parameters of the problem, the snail will eventually either leave the well or slide back to the bottom of the well. (In other words, the snail's height will exceed the height of the well or become negative.) You must find out which happens first and on what day.
Input
The input file contains one or more test cases, each on a line by itself. Each line contains four integers H , U , D , and F , separated by a single space. If H = 0 it signals the end of the input; otherwise, all four numbers will be between 1 and 100, inclusive. H is the height of the well in feet, U is the distance in feet that the snail can climb during the day, D is the distance in feet that the snail slides down during the night, and F is the fatigue factor expressed as a percentage. The snail never climbs a negative distance. If the fatigue factor drops the snail's climbing distance below zero, the snail does not climb at all that day. Regardless of how far the snail climbed, it always slides D feet at night.
Output
For each test case, output a line indicating whether the snail succeeded (left the well) or failed (slid back to the bottom) and on what day. Format the output exactly as shown in the example.
Sample Input
6 3 1 10 10 2 1 50 50 5 3 14 50 6 4 1 50 6 3 1 1 1 1 1 0 0 0 0
Sample Output
success on day 3 failure on day 4 failure on day 7 failure on day 68 success on day 20 failure on day 2
题意:一只小蜗牛想爬出井,已知井的深度为H,蜗牛白天能爬U米,晚上休息下滑D米,蜗牛的疲劳因子为F(即蜗牛每天往上爬的距离总比前一天少F%*U米),问蜗牛能否爬上井口,如果能,问第几天能够爬上井口,如果不能,问第几天失败。注意,这里的失败是指蜗牛“爬”到了井底。。。。。。
题解:一个循环模拟的过程,记录总路程和前一天走的距离(用于计算当天走的距离),如果总路程大于井的深度,则在这一天蜗牛成功走出井口,如果总路程小于0,则代表蜗牛滑到了井底,在这一天失败了。注意要分开判断,白天蜗牛再往上爬,判断是否达到井口,晚上蜗牛下滑,判断是否滑到了井底。
特别注意:如果某一天走的距离小于0了,就不要再让总数加上这个负数了,因为如果加上的话,后面还会减去晚上下降的,所以相当于多减了,会造成失败天数产生误差。
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
double H,U,D,F;
double s,sum;
int i;
while(scanf("%lf%lf%lf%lf",&H,&U,&D,&F),H)
{
i = 0;
s = U;
sum = 0;
while(1)
{
i++;
if(s > 0)//判断每天上升的距离大于0才会使总距离增加,不然就不会往上爬,当然也不会往下爬
sum = sum + s;//如果没有上面的判断,则相当于有了向下爬的动力,这样和蜗牛的意愿就背道而驰了
if(sum > H)
{
break;
}
sum = sum - D;
if(sum < 0)
{
break;
}
s = s - F / 100 * U;
}
if(sum > H)
printf("success on day %d\n",i);
else
printf("failure on day %d\n",i);
}
return 0;
}
本文探讨了一个经典的蜗牛爬井数学问题,通过编程的方式模拟蜗牛每天爬升和下滑的过程,最终确定蜗牛是能够成功爬出井口还是失败滑回底部,并给出具体天数。
684

被折叠的 条评论
为什么被折叠?



