1. 程式人生 > >PAT--1008 Elevator (20 分)

PAT--1008 Elevator (20 分)

1008 Elevator (20 分)

這個題目簡直就是超級無敵大水題啊!!!難度為0。。。。
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個正數。數字表示電梯將以指定順序停在哪些樓層。將電梯向上移動一層需要6秒鐘,向下移動一層樓需要4秒鐘。電梯將在每個站點停留5秒鐘。

對於給定的請求列表,您將計算在列表上完成請求所花費的總時間。電梯在開始時位於0樓,並且在滿足要求時不必返回到底層。

輸入規格:
每個輸入檔案包含一個測試用例。每個案例都包含一個正整數N,接著是N個正數。輸入中的所有數字都小於100。

輸出規格:
對於每個測試用例,在一行上列印總時間。

樣本輸入:
3 2 3 1
樣本輸出:
41

廢話不多說, 直接上程式碼!!

 #include<cstdio>
using namespace std;
int main()
{
	int floor = 0;
	int sum = 0;
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int stop_floor;
		scanf("%d", &stop_floor);
		if (stop_floor > floor)
		{
			sum += (stop_floor - floor) * 6; //上升的時候加6
			floor = stop_floor;
		}
		else if (floor > stop_floor)
		{
			sum += (floor - stop_floor) * 4;
			floor = stop_floor;
		}
		sum += 5;
	}
	printf("%d", sum);
}