原题
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
思路
题意:我们城市最高的建筑只有一部电梯。请求列表由N个正数组成。每个数字表示当前电梯停靠的层数。电梯上升一层需要6秒,下降一层需要4秒。电梯每次在相应楼层停5秒。
输入:包含一个正整数N,紧跟着N个正整数,每个输入的数字小于100。
输出:输出总时间。
这题,太easy了。直接写代码就行了。
题解
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
int up = 6, stay = 5, down = 4;
vector<int> vec(n);
for (int i = 0; i < n; ++i) {
int temp;
cin >> temp;
vec[i] = temp;
}
int times = vec[0] * up + stay;
for (int i = 1; i < n; ++i) {
if (vec[i] > vec[i - 1]) {
times += (vec[i] - vec[i - 1]) * up;
} else {
times += (vec[i - 1] - vec[i]) * down;
}
times += stay;
}
cout << times << endl;
return 0;
}