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

119 篇文章 0 订阅
8 篇文章 0 订阅

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<iostream>
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;
}

  • 16
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明仔的阳光午后

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值