@[TOC](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 1
Sample Output:
41
题意
先输入一个数n
后接n个数,代表这一层楼有人按了电梯
电梯初始在第0层,上升一层要6秒,下降一层要4秒,每一次停下都要5秒。
计算满足输入的需求所需时间
思路
模拟题
注意每一层都要停5s,最后一个需求也不例外
比如样例3 2 3 1
需求为2 3 1
所以答案为
26+5 16+5 2*4+5
即17+11+13=41
代码
#include<iostream>
using namespace std;
int main(){
int n,flr=0,sum=0;
cin>>n;
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
sum+=5;
if(tmp-flr>0){
sum+=6*(tmp-flr);
}else {
sum+=4*(flr-tmp);
}
flr=tmp;
}
cout<<sum;
return 0;
}