1008. Elevator (20)
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 1Sample Output:
41
这是一道简单题,计算电梯的运行时间,每上一层需要6秒,下一层需要4秒,每停靠一层要5秒,我们用vector<int> 类型的变量layer保存电梯的层次,由于刚开始是停靠在0th,所以layer第一个数为0,然后当layer[i]-layer[i-1]大于0时×6,当layer[i]-layer[i-1]小于0时×4,最后不要忘记加上每层停靠的时间。
#include<string>
#include<vector>
#include<iostream>
using namespace std;
int main(){
int num,temp,i,time = 0;
vector<int> layer;
layer.push_back(0);
cin>>num;
for ( i = 1 ; i <= num ; i++ ){
cin>>temp;
layer.push_back(temp);
if ( layer[i] - layer[i-1] > 0 ){
time += ( 6*( layer[i] - layer[i-1] ) );
}else{
time += ( 4*( layer[i-1] - layer[i] ) );
}
}
time += (5*(layer.size()-1));
cout<<time;
return 0;
}