题目:
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个正整数的列表,表示(denote)电梯在哪一层停下。上升(move...up)花费6s,下降(move down)花费4s,每一层停五秒。电梯一开始在0层,达到要求层时不必返回(return to)底层。
输入时,先输入一个整数n表示要输入的楼层数量,后面输入n个整数,每个数小于100;输出一共花费多少时间。
解题思路:用一个变量f记录现在所在的楼层f,一个变量记录输入的楼层temp,判断彼此的大小,temp>f时上升,<f时下降。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,f=0,temp,sum=0;
cin>>n;//接收n 没啥用,也可将while写成(while(n--))
while(cin>>temp){
if(temp>f){
sum+=(temp-f)*6+5;
f=temp;
}else{
sum+=(f-temp)*4+5;
f=temp;
}
}cout<<sum;
return 0;
}