题意
给定一个数组,表示电梯要停的楼层。电梯一开始在0层。每个停要花费5秒,上升一层6秒,下降一层4秒,求总共要多少秒。
思路
水~
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<int> a(n + 1);
int ans = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
ans += 5;
if (a[i] == a[i - 1]) continue;
if (a[i] > a[i - 1])
ans += 6 * (a[i] - a[i - 1]);
else
ans += 4 * (a[i - 1] - a[i]);
}
cout << ans << '\n';
return 0;
}
HINT
不定时更新更多题解,详见 git ! ! !