PAT甲级1008 Elevator (20分)

1008 Elevator (20分)

题目

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 Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41
题目大意

在一个城市里,最高的建筑就是一个电梯。题目的要求是要给你一串数字,表示电梯中的人将要去的层数,你需要计算总共需要的时间,电梯上升一层的时间为6s,下降一层的时间是4s,在一层停留的时间为5s。输入首先为一个数字N,表示有数字串中总共有多少个数字,每个数字不超过一百,输出为所需时间。

题目分析

根据每一层需要的时间,不同情况进行讨论,最后得出结果。

代码
#include "stdio.h"
int main(){
    int N;
    int i;
    int a[1005];
    int thisfloor;
    int sum;
    scanf("%d",&N);//总共有几个数字
    for (i = 0;i < N;i++){
        scanf("%d",&a[i]);//将数字串存储在int型的数组中,计算时,每次取一个。
    }
    thisfloor = 0;
    sum  = 0;
    for (i = 0;i < N;i++){//根据不同情况进行计算
        if (a[i] - thisfloor > 0) sum += 6*(a[i] - thisfloor) + 5;//上楼时间为6*层数+5
        if (a[i] - thisfloor == 0) sum += 5;//如果相同楼层,则加上停留时间5s
        if (a[i] - thisfloor < 0) sum += 4*(thisfloor - a[i]) + 5;//下楼时间为4*层数+5
        thisfloor = a[i];//计算结束后更新楼层
    }
    printf("%d",sum);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值