Elevator Trouble解题报告

题目摘要:You are on your way to your firstjob interview as a program tester, and you are already late. The interview isin a skyscraper and you are currently in floor s, where you see an elevator.Upon entering the elvator, you learn that it has only two buttons, marked"UP u" and "DOWN d". You conclude that the UP-button takesthe elevator u floors up (if there aren't enough floors, pressing theUP-botton does nothing, or at least so you assume), whereas the DOWN-buttontakes you d stories down (or none if there aren't enough). Knowing that theinterview is at floor g, and that there are only f floors in the building,you quickly decide to write a program that gives you the amount of buttonpushes you need to perform. If you simply cannot reach the correct floor, yourprogram halts with the message "use the stairs".

Given input f, s, g, u and d (floors,start, goal, up, down), find the shortest sequence of button presses you mustpress in order to get from s to g, given a building of f floors, or output"use the stairs" if you cannot get from s to g by the given elevator.

题目大意:坐电梯问题,一栋楼高f,你位于s层,面试地点在g层,电梯向上一次u层,向下一次d层。要求求能否坐电梯从你位于的层到面试的层,如果能,算出按电梯的最少次数。

输入输出要求

Input

The input will consist of one line, namelyf s g u d, where 1 <= s, g <= f <= 1000000 and 0 <= u, d <=1000000. The floors are one-indexed, i.e. if there are 10 stories, s and g bein [1, 10].

Output

You must reply with the minimum numbers ofpushes you must make in order to get from s to g, or output use the stairs ifit is impossible given the configuration of the elvator.

 

输入输出样例

Sample Input

10 1 10 2 1

100 2 1 1 0

 

Sample Output

6

use the stairs

 

解题思路:分类讨论,当s和g相等(即面试楼层就是你所在的楼层)时,显然结果是0。当s小于g(即面试地点在你所在楼层的上面)时,如果当d为0,且无法刚好上到所要去的楼层,那么只能走楼梯了。当然,如果u为0,或者u大于f,显然也是只能走楼梯。下面主要讨论可以坐电梯的情况。首先上升的不能上升为止,然后向下再向上,直到恰好能到所要到达的楼层。当s大于g时与此类似。具体算法看代码,相信很好懂。

代码

#include<iostream>

using namespace std;

int main()

{

   int f,s,g,u,d;

   while(cin>>f>>s>>g>>u>>d)

    {

       int mod,sum,i;

       if(s==g)

       {

           cout<<0<<endl;

       }

       else if(s<g)

       {

           if((u==0)||(u>f))

           {

                cout<<"use thestairs"<<endl;

                continue;

           }

           sum=(g-s)/u;

           mod=(g-s)%u;

           if((mod!=0)&&(d==0))

            {

                cout<<"use thestairs"<<endl;

                continue;

           }

           if(mod==0)

           {

                cout<<sum<<endl;

                continue;

           }

           for(i=1;;i++)

           {

                if((mod+i*d)%u==0)

                {

                   cout<<sum+i+(mod+i*d)/u<<endl;

                    break;

                }

                if(mod+i*d>f)

                {

                    cout<<"use thestairs"<<endl;

                   break;

                }

           }

       }

       else if(s>g)

       {

           if((d==0)||(d>f))

           {

                cout<<"use thestairs"<<endl;

                continue;

           }

           sum=(s-g)/d;

           mod=(s-g)%d;

           if((mod!=0)&&(u==0))

           {

                cout<<"use thestairs"<<endl;

                continue;

           }

           if(mod==0)

           {

                cout<<sum<<endl;

                continue;

           }

           for(i=1;;i++)

           {

                if((mod+i*u)%d==0)

                {

                   cout<<sum+i+(mod+i*u)/d<<endl;

                    break;

                }

                if(mod+i*u>f)

                {

                    cout<<"use thestairs"<<endl;

                    break;

                }

           }

       }

    }

   return 0;

}

解题感想:这题WA了好几遍,主要是考虑的情况太多,漏了几种情况。也或许是我分类的标准过于复杂,导致增加了问题难度。总之,所有的情况都讨论清楚AC了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值