Elevator解题报告

题目摘要:The highest building in our cityhas only one elevator. A request list is made up with N positive numbers. Thenumbers denote at which floors the elevator will stop, in specified order. Itcosts 6 seconds to move the elevator up one floor, and 4 seconds to move downone floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfillthe requests on the list. The elevator is on the 0th floor at the beginning anddoes not have to return to the ground floor when the requests are fulfilled.

题目大意:电梯问题,一架电梯位于第0层,上升一层用6秒,下降一层用4秒,停在一层用5秒,给出一串命令求所用时间。

输入输出要求

Input

There are multiple test cases. Each casecontains a positive integer N, followed by N positive numbers. All the numbersin the input are less than 100. A test case with N = 0 denotes the end ofinput. This test case is not to be processed.

 

Output

Print the total time on a single line foreach test case.

 

输入输出样例

Sample Input

1 2

3 2 3 1

0

 

Sample Output

17

41

 

解题思路:直接开一个数组,比较第i项和前一项的大小,然后加上相应的时间。

代码

#include<iostream>

using namespace std;

int num[105];

int main()

{

    int N;

    num[0]=0;

    while(cin>>N)

    {

           if(N==0)

              return0;

           else

           {

              int i;

              int sum=0;

              for(i=1;i<=N;i++)

              {

                  cin>>num[i];

                  if(num[i]>num[i-1])

                       sum=sum+6*(num[i]-num[i-1])+5;

                       else if(num[i]==num[i-1])

                           sum=sum+5;

                       else if(num[i]<num[i-1])

                           sum=sum+4*(num[i-1]-num[i])+5;

              }

              cout<<sum<<endl;

           }

    }

    return 0;

}

解题感想:水题一枚,不想多说。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值