PAT 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

题目大意:一栋大楼里面只有一部电梯,初始在0楼,上升一楼要6秒,下降一楼要4秒, 每一楼要停留5秒,现在给出一串电梯停靠的楼层,计算按照顺序停靠完,需要多少时间
输入:第一个数字是需要停靠的次数,后面跟着停靠的楼层
last:上一次停靠的楼层
now:这一次停靠的楼层
ans: 记录所需时间
 1 #include<iostream>
 2 using namespace std;
 3 int main(){
 4   int n, last = 0, now, ans = 0;
 5   cin>>n;
 6   for(int i=0; i<n; i++){
 7     cin>>now;
 8     if(now>last) ans += (now-last)*6;
 9     else ans += (last-now)*4;
10     ans += 5;
11     last = now;
12   }
13   cout<<ans;
14 }