[PTA] PAT(A) 1008 Elevator (20 分)

目录

  • Problem

    • Description
    • Input
    • Output
    • Sample
      • Sample Input
      • Sample Output
  • Solution
    • Analysis
    • Code

Problem

portal: 1008 Elevator (20 分)

Description

 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

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

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

Sample

Sample Input

3 2 3 1

Sample Output

41

Solution

Analysis

?电梯向上走一层需要6s,向下走一层需要4s,每一次停止时停留5s,最开始电梯是在第0层,结束后电梯无需返回底层。所以在给出了列表之后,只需要判断电梯是向上还是向下,然后用对应的楼层差值乘所需要的时间,便是电梯移动需要的时间花费,最后加上停留需要的时间,将列表中的每一个楼层都处理完,便是整体所需要的时间。

Code

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

int main(void) {
    int n, last, now; // last是上一次停留的楼层, now是这次要去的楼层
    int result = 0;

    cin >> n;
    last = 0;
    for (int i = 0; i < n; i++) {
        cin >> now;
        if (now > last) { // 此时电梯向上运动
            result += 6 * (now - last) + 5;
        } else { // 此时电梯向下运动
            result += 4 * (last - now) + 5;
        }
        last = now; // 当前停留楼层更新
    }
    cout << result << endl;
}

原文地址:https://www.cnblogs.com/by-sknight/p/11444492.html

时间: 2024-10-12 10:36:20

[PTA] PAT(A) 1008 Elevator (20 分)的相关文章

1008 Elevator (20分)

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

【PAT甲级】1008 Elevator (20分)

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

【PAT甲级】1008 Elevator (20 分)

题意: 电梯初始状态停在第0层,给出电梯要接人的层数和层序号,计算接到所有人需要的时间,接完人后电梯无需回到1层(1层不是0层).电梯上升一层需要6秒,下降一层需要4秒,接人停留时间为5秒. 代码: #include<bits/stdc++.h> using namespace std; int a[100007]; int main(){ int n; cin>>n; int ans=0; for(int i=1;i<=n;++i){ cin>>a[i]; if

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 seco

PAT:1008. Elevator (20) AC

#include<stdio.h> int main() { int n,ans=0,now=0; //要停n层,ans是总时间,now代表当前层数 scanf("%d",&n); for(int i=0 ; i<n ; ++i) { int tmp; scanf("%d",&tmp); if(tmp>now) //上楼,每上一层6秒 { ans+=(tmp-now)*6; now=tmp; } else if(tmp<

1008. Elevator (20)——PAT (Advanced Level) Practise

题目信息: 1008. Elevator (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 w

Programming Ability Test学习 1008. Elevator (20)

1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 st

pat 1035 Password(20 分)

1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) fro

pat 1077 Kuchiguse(20 分) (字典树)

1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often