强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬
本文由参考于柳神博客写成
PS
题目原文
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个数字,N个数字就是电梯的顺序.
上一层需要6 秒,下一层需要4 秒
然后每到一层还要停留5 秒
问,电梯结束后,一共需要多少秒—PS 电梯结束后,不需要回到0层
一开始电梯在0层
代码如下:
#include<iostream>
#include<vector>
#include<string>
#include<inttypes.h>
using namespace std;
int main(void) {
int begin = 0, N = 0;
int index = 0;
int total = 0;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> index;
if (index > begin) {
total += (index - begin) * 6 + 5;
}
else if (index == begin)
total += 5;
else {
total += (begin - index) * 4 + 5;
}
begin = index;
}
cout << total;
return 0;
}
如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗
相信我,你也能变成光.
如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.