PTA (Advanced Level) 1008 Elevator

Elevator

  The highest building in our city has only one elevator. A request list is made up with Npositive 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 Npositive 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秒,电梯最后不需要返回0层。

  模拟一下电梯的运行过程即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n, t = 0, nowf = 0; //n为电梯需停留层数, t为已经消耗的时间, nowf是电梯当前所在的楼层
 4 int main()
 5 {
 6     scanf("%d", &n);    //输入电梯需停留层数
 7     while(n--){
 8         int floors;
 9         scanf("%d", &floors);   //输入下一个要停留的楼层
10         while(nowf < floors){   //向上运行
11             nowf++;
12             t += 6;
13         }
14         while(nowf > floors){   //向下运行
15             nowf--;
16             t += 4;
17         }
18         t += 5; //停留
19     }
20     printf("%d\n", t);
21     return 0;
22 }

 

posted @ 2019-04-08 17:59  suvvm  阅读(296)  评论(0编辑  收藏  举报