PTA(Advanced Level)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
思路

题目的大意是给定一个电梯的请求序列,按照序列要求运行要花费多少时间。给定的信息有①每爬一层+6s②每下一层+4s③每一层停留5s④最开始在第0层,最后不用回到第0层

知道了以上的信息手动模拟即可

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

int main()
{
	int time = 0;	//表示总耗时
	int floor = 0;	//表示当前位于哪一层
	int n, target;
	cin >> n;
	while(n--)
	{
		cin >> target;
		if(target > floor)
		{
			time += 6*(target - floor);		//每爬一层+6s
			time += 5;
			floor = target;
		}else
		{
			time += 4*(floor - target);		//每下一层+4s
			time += 5;
			floor = target;
		}
	}
	cout << time;
	return 0;
}

引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805511923286016

原文地址:https://www.cnblogs.com/MartinLwx/p/12498503.html