HDU 1008 (Elevator)

本文介绍了一个关于电梯运行时间计算的问题,电梯从0层开始,根据输入的楼层指令,计算总的运行时间,包括上升、下降及停留时间。文章提供了详细的算法分析和C++代码实现。

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

                              Elevator

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 87063 Accepted Submission(s): 47582

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 2
3 2 3 1
0
Sample Output:17
41
Author ZHENG, Jianqiang
题目大意:有一个很高的楼,开始电梯处于零层,电梯运行的方式就是:上升一层需要的时间是6s,下降一层所需要的时间是4S,每层的停留时间是5S。给出电梯的运行楼层指令,输出运行时间。

分析样例输入输出:
第一组数据:1 2 表示1个数据 2
1、零层到2层12S 总计12S
2、停留5S 总计17S
第二组数据:3 2 3 1
1、零层到2层 需要时间12S 总计12S
2、停留5S 总计:17S
3、2层到3层 需要时间 6S 总计:23S
4、停留时间 5S 总计:28S
5、3层到1层 需要时间8S 总计36S
6、停留时间 5S 总计41S
第三组数据: 0
不做处理

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string.h>
using namespace std;
int n;//每个样例的指令数目
int main(){
    while(~scanf("%d",&n)&&n){
        int last = 0;//用来记录当前楼层
        int temp;//用来记录将要前往楼层
        int sum = 0;//用来记录总计的时间
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            if(temp>last){//如果是上升的话
                sum+=(temp-last)*6;
            }
            else{//如果是下降的话
                sum+=(last-temp)*4;
            }
            //暂停时间
            sum+=5;
            last = temp;
        }
        //输出格式每个样例输出一行
        printf("%d\n",sum);
    }
    return 0;
}

题解代码:


#include<stdio.h>
int n;
int main()
{
    int x;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        int now=0,ans=0;//now表示当前电梯所在楼层
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&x);
            if(x>now) ans+=(x-now)*6;
            else if(x<now)ans+=(now-x)*4;
            now=x;
        }
        printf("%d\n",ans+5*n);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值