Elevator
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.
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
1 #include <stdio.h> 2 3 int main(){ 4 int number; 5 int floor; 6 int position; 7 int time; 8 int temp; 9 10 while(1){ 11 scanf("%d",&number); 12 13 if(number==0) 14 break; 15 16 position=0; 17 time=0; 18 while(number--){ 19 scanf("%d",&floor); 20 21 if(floor==position) //如果请求的楼层为当前楼层,则不用移动,时间加上电梯停留的时间 22 time+=5; 23 24 else if(floor>position){ //电梯要往上升,计算移动时间和等待时间,改变当前楼层 25 temp=(floor-position)*6+5; 26 time+=temp; 27 position=floor; 28 } 29 30 else{ //电梯要往下降,计算移动时间和等待时间,改变当前楼层 31 temp=(position-floor)*4+5; 32 time+=temp; 33 position=floor; 34 } 35 } 36 37 printf("%d\n",time); 38 } 39 40 return 0; 41 }