1. 程式人生 > >PAT甲級 1008.Elevator(20) 題目翻譯與答案

PAT甲級 1008.Elevator(20) 題目翻譯與答案

題目來源自PAT網站  https://www.patest.cn/

題目描述:

1008. Elevator (20)

The highestbuilding 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, inspecified order. It costs 6 seconds to move the elevator up one floor, and 4seconds to move down one floor. The elevator will stay for 5 seconds at eachstop.

For a given requestlist, you are to compute the total time spent to fulfill the requests on thelist. The elevator is on the 0th floor at the beginning and does not have toreturn to the ground floor when the requests are fulfilled.

InputSpecification:

Each input filecontains one test case. Each case contains a positive integer N, followed by Npositive numbers. All the numbers in the input are less than 100.

OutputSpecification:

For each test case,print the total time on a single line.

SampleInput:

3 2 3 1

SampleOutput:

41

時間限制

400 ms

記憶體限制

65536 kB

程式碼長度限制

16000 B

判題程式

Standard

作者

CHEN, Yue

題目翻譯:

1008.電梯

在我們的城市裡,最高的建築物裡只有一部電梯。有一份由N個正陣列成的請求列表。這些數表示電梯將會以規定的順序在哪些樓層停下。電梯升高一層需要6秒,下降一層需要4秒。每次停下電梯將花費5秒。

給你一個請求列表,你需要計算出完成列表裡的請求總共花費的時間。一開始電梯在0層。當請求全部完成時,電梯不需要回到底層。

輸入說明:

每個輸入檔案包含一個測試例項。每個例項包含一個正整數N,後面跟著N個數字。所有輸入的數字小於100。

輸出說明:

對於每個測試例項,在一行中輸出總時間。

樣例輸入:

3 2 3 1

樣例輸出:

41

答案程式碼:

#include<cstdio>
int main()
{
	int stay=0,t;
	int N,i,total=0;
	scanf("%d",&N);
	for(i=0;i<N;++i)
	{
		scanf("%d",&t);
		if(t==stay)
		{
			total+=5;
			continue;
			
		}
		if(t>stay)
			total+=(t-stay)*6+5;
		else
			total+=(stay-t)*4+5;
		stay=t;
	}
	printf("%d",total);
	return 0;
}