1408201941-hd-Elevator.cpp

本文介绍了一个关于电梯路径时间计算的问题,电梯从0层出发,向上移动每层花费6秒,向下移动每层花费4秒,并在每个指定楼层停留5秒。文章提供了计算经过一系列楼层所需总时间的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Elevator

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4333 Accepted Submission(s): 2170


 

Problem Description

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one 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 fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

 


 

Input

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

 


 

Output

Print the total time on a single line for each test case.

 


 

Sample Input

1 23 2 3 10

 


 

Sample Output

1741

 


 

 

题目大意

       这是一个关于电梯的问题。电梯每次都是从0层开始,向上每一层花的时间6,向下每一层花的时间为4,电梯在每一层需要停止的楼层会停留5,求经过所有需要停留的楼层之后所用的总时间。

 

注意事项

       结束的时间并不是电梯到达最后一个需要停留的楼层的时候,还需要再加上在最后一个需要停留的楼层停留所用的时间5.

 

代码

<span style="font-size:18px;">#include<stdio.h>
int s[110];
int main()
{
    int n;
    int i,j,k,sum;
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)
            scanf("%d",&s[i]);
        sum=n*5;
        j=0;
        for(i=0;i<n;i++)
        {
            if(s[i]>j)
                sum+=(s[i]-j)*6;
            else if(s[i]<j)
                sum+=(j-s[i])*4;
            j=s[i];
        }
        printf("%d\n",sum);
    }
    return 0;
}
</span>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值