版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42449444/article/details/88782541
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.
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.
For each test case, print the total time on a single line.
3 2 3 1
41
这道水题在18年HBU校赛给的模拟题库里有 就是数值不一样而已,电梯上升一层需要6秒,电梯下降一层需要4秒,到达指定楼层需要停5秒。无脑用自定义函数getTime()来分段对每一次电梯运行进行求解就行了。
#include <bits/stdc++.h>
using namespace std;
int getTime(int start,int end) //求start到end的运行时间
{
int time = 0;
if(start > end) //电梯下降需要4秒一层
{
time += 4*(start-end);
}
else //电梯上升需要6秒一层
{
time += 6*(end-start);
}
time += 5; //到达指定楼层停5秒
return time;
}
int main()
{
int N;
cin >> N;
int a[N];
int start = 0, end; //start为当前楼层,end为目标楼层
int time = 0; //电梯总用时
for(int i = 0; i < N; i++)
{
cin >> end;
time += getTime(start,end);
start = end;
}
cout << time << endl;
return 0;
}