Categories
不学无术

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

==============================================
此题初看还以为是电梯算法,其实啥都不是。。。。
注意的是第一个输入的数字是后面的总的输入个数,不是别的其他什么…
答案也很短…好久没碰到这么简单的了
==============================================

#include 
using namespace std;
int main(int argc, char* argv[])
{
	int N;
	cin >> N;
	int floor_now = 0; //当前在0层
	int next_floor = 0;
	int total_time = 0;
	for(int i=0; i> next_floor;
		int time_per_floor = (next_floor - floor_now > 0) ? 6 : -4; //上楼还是下楼
		total_time = total_time + time_per_floor * (next_floor - floor_now) + 5;
		floor_now = next_floor;
	}
	cout << total_time;
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.