Elevator
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个楼层,要求电梯到达所有楼层。电梯初始楼层为0,向上1楼要6s,向下1楼要4s,在每层停留时间为5s。要求计算一共所用时间。
思路
题目较简单,依次计算即可。
代码实现
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int n, x;
int level = 0;
int d;
int time = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
d = x - level;
if (d >= 0)
time += d * 6 + 5;
else
time += abs(d) * 4 + 5;
level = x; // 注意不要忘了更新当前所处楼层
}
printf("%d", time);
return 0;
}
本文介绍了一个简单的算法问题:计算电梯按指定楼层顺序运行所需的总时间。电梯从0楼出发,向上移动一层需6秒,向下移动一层需4秒,每层停留5秒。通过实例演示了如何计算总运行时间。
2805

被折叠的 条评论
为什么被折叠?



