Elevator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 59578 Accepted Submission(s): 32699
Problem 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.
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 秒。电梯每次开始都是从第 0 层开始往上升的。
输入有多组数据以 0 结束输入,每次输入的第一个数字,代表的是有多少条电梯停靠指令,后面便是输入的指令。
思路:
一看题目我们首先便觉得是要去模拟出电梯的升降过程,升多少层就乘以多少个 6 ,降多少层就乘以多少个 4,并且停靠了几次就乘以多少个 5.那么题目就很简单了,前面楼层数比后面楼层数高的时候 电梯肯定是下降过程的,反之则是上升过程的。
给出AC代码:
#include<iostream>
using namespace std;
int f(int x) //取绝对值函数,也可以包含数学头文件#include<cmath> 使用绝对值函数 abs();
{
if (x < 0)return -x;
else return x;
}
int main()
{
int n, floor[10000];
while (cin>>n)
{
if (n == 0)break;
for (int i = 0; i < n; i++)
cin >> floor[i];
int sum = floor[0] * 6;
for (int i = 1; i < n; i++)
{
if (floor[i - 1] > floor[i])sum += (floor[i - 1] - floor[i]) * 4;
else sum += f(floor[i - 1] - floor[i]) * 6;
}
cout << sum + 5*n << endl;
}
return 0;
}