PAT-Apat甲级题1008(python和c++实现)

PTA | 1008 Elevator

1008 Elevator

作者 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 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楼,当要求得到满足时不必返回地面楼层。
输入规范:

每个输入文件包含一个测试用例。每个case都包含一个正整数N,后面跟着N个正数。输入中的所有数字都小于100。
输出规格:

对于每个测试用例,在一行上打印总时间。
样品输入:

3 2 3 1

输出示例:

41

一遍题目读下来,可以提取到以下的信息:

        1, 输入一行,但是分为两个部分,第一个为电梯所经过的楼层数量,其余为具体楼层

        2, 上楼和下楼所花费的时间不一样,每层设置一个停留时间,且停留时间相同

        3, 电梯起始层数为0层,且结束后不必回到一层

综合上述内容,本题难度较低,重点是在于细节部分的处理,okk,接下来是熟悉的手搓代码时间!!!

首先,定义输入变量和输入数组,确定初始变量:上升一层所花费时间up=6,下降一层所需时间low=4,停留时间stop=5,定义cost用于记录花费时间,定义lastflow用于记录上一次的楼层位置

接下来,循环接收输入的楼层,并根据楼层变化确定花费的时间,值得注意的是,千万不要遗忘了,楼层下降时,计算得到的楼层差为负数,计算花费时间时需要注意这点!

由于本题思路较为简单,下面直接给出完整的代码,若有疑惑之处,欢迎评论区交流!

python:

lst = [int(i) for i in input().split()][1:]
up = 6
low = 4
stop = 5
lastflow = 0
cost = 0
for i in lst:
    cha = i - lastflow
    if cha > 0:
        cost += cha * up
    elif cha < 0:
        cost += -cha * low
    cost += stop
    lastflow = i
print(cost)

C++:


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

int n;
int up=6,low=4,stop=5,cost,lastflow;

int main(){
    cin >> n;
    lastflow = 0;
    for(int i=0; i<n; i++){
        int temp;
        cin >> temp;
        int cha = temp - lastflow;
        if(cha > 0){
            cost += cha * up;
        }
        else{
            cost += (-cha) * low;
        }
        cost += stop;
        lastflow = temp;
    }
    cout << cost;
    
}

最后附上AK截图:

C++:

python:

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-05 23:34:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-05 23:34:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-05 23:34:01       82 阅读
  4. Python语言-面向对象

    2024-02-05 23:34:01       91 阅读

热门阅读

  1. 从零学算法162

    2024-02-05 23:34:01       50 阅读
  2. 【从浅到深的算法技巧】堆的定义

    2024-02-05 23:34:01       35 阅读
  3. Python循环语句——while循环的嵌套应用

    2024-02-05 23:34:01       39 阅读
  4. Spring设计模式之工厂方法

    2024-02-05 23:34:01       54 阅读
  5. 游戏如何选择服务器

    2024-02-05 23:34:01       62 阅读
  6. Linux笔记之bash脚本中的$符号

    2024-02-05 23:34:01       45 阅读
  7. 淘客返利系统:揭秘技术背后的实现方案

    2024-02-05 23:34:01       56 阅读
  8. LeetCode--代码详解 292.Nim游戏

    2024-02-05 23:34:01       50 阅读
  9. 如何创建和使用视图?

    2024-02-05 23:34:01       54 阅读
  10. 【ASP.NET Core 基础知识】--Web API--RESTful设计原则

    2024-02-05 23:34:01       45 阅读
  11. Docker

    Docker

    2024-02-05 23:34:01      43 阅读
  12. Postgresql PostGIS扩展

    2024-02-05 23:34:01       45 阅读