【一只蒟蒻的刷题历程】【PAT (Advanced Level) Practice】 1008 电梯

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

扫描二维码关注公众号,回复: 11174049 查看本文章

样本输出:

41


代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int maxn=10100;
int main() 
{
   int n,now=0,x; //n个操作,开始在第0层,x表示下一个操作的层
   cin>>n;
   int sum=5*n;  //每层停5s
   
   for(int i=0;i<n;i++)
   {
   	  cin>>x;
   	  if(x>now) 
   	   sum += (x-now)*6;
   	  else 
   	   sum += (now-x)*4;
   	  now=x;
   } 
   cout<<sum;
    return 0;
}

原创文章 32 获赞 35 访问量 1516

猜你喜欢

转载自blog.csdn.net/weixin_45260385/article/details/105901220
今日推荐