很水的一道题,不过有个坑

计算电梯完成给定顺序所需时间

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

实现思想

只需要根据输入的楼层和上一个楼层相比较

如果下降则加上层数*4秒

如果上升则加上层数*6秒

之后再加上5秒停留时间

坑点:==如果序列中相邻两个楼层相同,则必须加5秒==

不能用平常的思想认为不需要一直停,直接就走了,不然有的测试点无法通过

还有:不要想太多认为到了序列最后一个楼层就不需要算停留的5秒(我不会告诉你我为什么知道并且特意减了5秒)

代码实现

#include <iostream>

using namespace std;

int main() {
    int n, last_floor = 0, floor, time = 0;
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> floor;
        if(floor > last_floor) {
            time += (floor - last_floor) * 6;
        } else if(floor < last_floor) {
            time += (last_floor - floor) * 4;
        }
        time += 5;
        last_floor = floor;
    }
    cout << time;

    return 0;
}