PAT-A 真题- 1008. Elevator

发布于 / PAT-甲级 / 0 条评论

原题干:

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,电梯在某层停留需要5秒。

给定的第一个数字是有层需要停,接着给出需要在第几层停。电梯初始位置在0层。

题目过于简单,不做详细解释。

#include <cstdio>

int main(){
    const int UP = 6, DOWN = 4, STAY = 5;
    int cnt;
    scanf("%d", &cnt);
    int it = 0, total = 0;
    while(cnt--){
        int _;
        scanf("%d", &_);
        if(_ > it) total += ((_ - it) * UP);
        else if(_ < it) total += ((it - _) * DOWN);
        it = _;
        total += STAY;
    }
    printf("%d", total);
    return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题- 1008. Elevator
目前还没有评论,快来抢沙发吧~