【C语言做题系列】The Snail

#今天做了一道题 ,来给大家分享一波。
Description

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:蜗牛的疲劳期,每天少爬U*F/100。
注意:
1、蜗牛每天爬的距离都比前一天少。
2、蜗牛每天爬的距离总是要大于0。
3、函数类型最好用double。
4、当蜗牛爬行的总路程大于井的高度时就成功了,当蜗牛下滑之后的高度小 于0就失败。

上述文字是copy

#include <stdio.h>

int main()
{
    double h, d, u, f;
    while(scanf("%lf%lf%lf%lf", &h, &u, &d, &f)!=EOF, h) //输入井的深度h,白天爬的距离u,晚上下降的距离d,疲劳系数f
    {
        double v  = u;                               //定义一个 v 来表示之后每天白天爬的距离
        int sum = 0;                                  //sum 计算天数
        double distance=0;                              //  定义distance 表示蜗牛向上爬得的距离
        while(1)
        {
            sum++;           
            if(v>0)                                //向上爬的距离必须为正,否则蜗牛原地不动
                distance = distance + v;           //若是爬的总距离>井深 则视作成功 跳出循环
            if(distance>h)
                break;
            distance = distance - d;                //无论每天是向上爬或者原地不动  蜗牛晚上都会向下掉
            if(distance<0)                          //若蜗牛重新掉回井底即爬得的总距离为负,则视作失败,跳出循环
                break;
            v = v-(f/100)*u;                      //蜗牛每天能向上爬得的距离总是前一天少, 而且是固定(f/100)*u  这么多
        }
        if(distance > h)
            printf("success on day %d\n",sum);
        else
            printf("failure on day %d\n",sum);

    }
    return 0;
}

注:上述思路是把蜗牛最终能爬的高度distance 设出来, 其实我们还可以换一种思路 ,我们可以设蜗牛距离井口的深度, 比如将之设成 变量 now,当now<0时即表示成功,当now>h时即表示失败。

#include <stdio.h>
int main()
{
    double h, d, u, f;
    while(scanf("%lf%lf%lf%lf", &h, &u, &d, &f)!=EOF)
    {
        if(h==0)                    
            break;
        int sum=1;                     //我们把第一天单独剥离
        double pilao = u*f/100;        //表示出之后每天爬行因疲劳而减少的距离
        double distance;                    //定义一个变量distance 表示 蜗牛距离井口的深度
        if(u>h)                              //若是蜗牛爬行的距离>井深  则第一天就能成功
            printf("success on day %d", sum);
        else                                    //否则,我们把第一天之后蜗牛距离井口的深度表示出来
        {
            distance = h-u+d;
            u-=pilao                            //此处我们把第二天蜗牛经疲劳系数削减后能爬行的距离表示出来
            while(1)
            {
                sum++;
                distance-=u;                      //以下 与前文大同小异
                u-=pilao;
                if(distance<0)
                {
                    printf("success on day %d\n", sum);
                    break;
                }
                distance+=d;
                if(distance>h)
                {
                    printf("failure on day %d\n", sum);
                    break;
                }
            }
        }
    }
    return 0;
}

注:因为此题是英文题,在经过网页翻译的时候题目可能理解出了偏差, 在这里在下特别提醒要注意一点:每天少爬U*F/100。 这是固定的, 并非每天少爬的距离是在前一天的基础上, 而是始终在第一天的基础上。

坚持学习,大家一起加油!

发布了16 篇原创文章 · 获赞 0 · 访问量 455

猜你喜欢

转载自blog.csdn.net/qq_45627679/article/details/104181012