• 「PAT甲级真题解析」Advanced Level 1008 Elevator


    PAT (Advanced Level) Practice 1008 Elevator

    如果对你有帮助,要点个赞让我知道喔~

    问题分析

    1. 题设要求模拟电梯的升降,并计算完成所有楼层停靠要求所需要的总时间。
    2. 由于电梯停考规则和停靠时间题设已经明确给出, 所以这是一道只需要我们按照规则翻译成代码的模拟题。

    完整描述步骤

    1. 获取输入: 楼层停靠请求数目

    2. 初始化记录器:

      • 当前楼层 = 0
      • 总计耗时 = 0
    3. 依次读入楼层停靠请求:

      • 如果要前往的楼层层次比当前楼层小:
        • 总计耗时 += (当前楼层 - 目标楼层) * 4 + 5;
      • 如果要前往的楼层层次比当前楼层大:
        • 总计耗时 += (目标楼层 - 当前楼层) * 6 + 5;
      • 如果要前往的就是当前楼层:
        • 总计耗时 += 5;
      • 设置读入的目标楼层为当前楼层
    4. 输出总计耗时

    伪代码描述

    1. get input: request_amount
    2. init recorders:
      • current_layer = 0
      • total_time_cost = 0
    3. for each request:
      • get input: target_layer
      • if target_layer < current_layer:
        • total_time_cost += (current_layer - target_layer) * 4;
      • elif target_layer > current_layer:
        • total_time_cost += (target_layer - current_layer) * 6;
      • total_time_cost += 5;
      • current_layer = target_layer;
    4. print(total_time_cost)

    完整提交代码

    /*
    # 问题分析
    1. 题设要求模拟电梯的升降,并计算完成所有楼层停靠要求所需要的总时间。
    2. 由于电梯停考规则和停靠时间题设已经明确给出, 所以这是一道只需要我们按照规则翻译成代码的模拟题。
    
    # 完整描述步骤
    1. 获取输入: 楼层停靠请求数目
    2. 初始化记录器:
        - 当前楼层 = 0
        - 总计耗时 = 0
    3. 依次读入楼层停靠请求:
        - 如果要前往的楼层层次比当前楼层小:
            - 总计耗时 += (当前楼层 - 目标楼层) * 4 + 5;
        - 如果要前往的楼层层次比当前楼层大:
            - 总计耗时 += (目标楼层 - 当前楼层) * 6 + 5;
        - 如果要前往的就是当前楼层:
            - 总计耗时 += 5;
        - 设置读入的目标楼层为当前楼层
    
    5. 输出总计耗时
    
    # 伪代码描述
    1. get input: request_amount
    2. init recorders:
        - current_layer = 0
        - total_time_cost = 0
    3. for each request:
        - get input: target_layer
        - if target_layer < current_layer:
            - total_time_cost += (current_layer - target_layer) * 4;
        - elif target_layer > current_layer:
            - total_time_cost += (target_layer - current_layer) * 6;
        - total_time_cost += 5;
        - current_layer = target_layer;
    4. print(total_time_cost)
    
    */
    
    # include
    using namespace std;
    
    int main(){
        int request_amount;
        cin >> request_amount;
        int current_layer = 0;
        int total_time_cost = 0;
        for (int i = 0; i < request_amount; i++){
            int target_layer;
            cin >> target_layer;
            if (target_layer > current_layer){
                total_time_cost += (target_layer - current_layer) * 6;
            } else if (target_layer < current_layer) {
                total_time_cost += (current_layer - target_layer) * 4;
            }
            total_time_cost += 5;
            current_layer = target_layer;
        }
        
        cout << total_time_cost;
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    平凡前端之路_16.变量的解构赋值
    2023年9月到2024年1月计划安排
    jupyter常用的方法以及快捷键
    防脱发、再生发
    腾讯升级员工关怀方案:入职满 15 年可提前退休
    【单线图的系统级微电网仿真】基于 PQ 的可再生能源和柴油发电机组微电网仿真(Simulink)
    Vue - 虚拟DOM的简单理解
    分享购是什么?分享购商业模式剖析解读
    Unity SteamVR 开发教程:SteamVR Input 输入系统(2.x 以上版本)
    国内备案的域名个人和企业的区别
  • 原文地址:https://blog.csdn.net/qq_41785288/article/details/127641324