1. 程式人生 > >【笨方法學PAT】1008 Elevator(20 分)

【笨方法學PAT】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

二、題目大意

電梯上下,停留5s、上升每層6s、下降每層4s,從0樓開始計算,每次不需要回到0樓。

三、考點

模擬

四、解題思路

1、讀入資料;

2、相鄰的兩層進行處理,如果上升按照上升的時間計算、下降按照下降的時間計算。vec[0] 作為0樓,可以按照0樓開始。

PS. 當然,也可以聯機演算法,在讀取資料的過程中直接進行處理,但我自己覺得,分開處理的話,思考的時間更多,出錯的可能性會降低。

五、程式碼

#include<iostream>
#include<vector>
using namespace std;
int main() {
	int n;
	//讀取資料
	cin >> n;
	vector<int> vec(n+1);
	vec[0] = 0;
	for (int i = 1; i <= n; ++i)
		cin >> vec[i];

	//處理資料
	int sum = 0;
	for (int i = 1; i <= n; ++i) {
		//停留時間
		sum += 5;

		//下降時間
		if (vec[i] - vec[i - 1] >= 0)
			sum += (vec[i] - vec[i - 1]) * 6;

		//上升時間
		else
			sum+= (vec[i-1] - vec[i]) * 4;
	}

	//輸出結果
	cout << sum << endl;
	system("pause");
	return 0;
}