[PAT A1008]Elevator(水题、数学问题)

1008 Elevator

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

Analysis:

电梯在0层开始,向上移动一层需要 6 秒,向下移动一层需要 4 秒。电梯将在每个stop floor停留5秒钟。是一道简单的数学模拟。

Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	int temp,pre=0;
	int sum=0;
	cin>>n;
	int t=n;
	while(n--)
	{
		cin>>temp;
		if(temp>pre)
			sum+=((temp-pre)*6)+5;
		else
			sum+=((pre-temp)*4)+5;
		pre=temp;
	}
	cout<<sum;
	return 0;
}
上一篇:BUAA_OO 第二单元总结


下一篇:A1008 Elevator (20分)_C语言_翻译+解析