The Legend of the Elevator(20210720练习赛D题题解)
Description
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
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
Output
Print the total time on a single line for each test case.
Sample Input
1 2
3 2 3 1
0
Sample Output
17
41
题目分析
一个电梯,上楼花6秒,下楼花4秒,到一层还得停5秒,给你给你多组数据,让你按照指定顺序到达不同的楼层,总共需要花多长时间?
这道题就是一个纯模拟题,比较这次操作和前次操作上楼还是下楼就行了。
写的时候最好把变量放到全局,且从下标1开始存,这样写会容易很多具体看下面的代码
AC代码
#include <bits/stdc++.h>
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rT printf("\nTime used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC)
using namespace std;
const int N = 1010;
int a[N], n;
int main() {
io;
while (cin >> n && n) {
for (int i = 1; i <= n; i++) cin >> a[i];
int res = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > a[i - 1]) {
res += (a[i] - a[i - 1]) * 6;
} else {
res += (a[i - 1] - a[i]) * 4;
}
res += 5;
}
cout << res << '\n';
}
return 0;
}