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

Table of Contents

问题分析

  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# 问题分析 31. 题设要求模拟电梯的升降,并计算完成所有楼层停靠要求所需要的总时间。 42. 由于电梯停考规则和停靠时间题设已经明确给出, 所以这是一道只需要我们按照规则翻译成代码的模拟题。 5 6# 完整描述步骤 71. 获取输入: 楼层停靠请求数目 82. 初始化记录器: 9 - 当前楼层 = 0 10 - 总计耗时 = 0 113. 依次读入楼层停靠请求: 12 - 如果要前往的楼层层次比当前楼层小: 13 - 总计耗时 += (当前楼层 - 目标楼层) * 4 + 5; 14 - 如果要前往的楼层层次比当前楼层大: 15 - 总计耗时 += (目标楼层 - 当前楼层) * 6 + 5; 16 - 如果要前往的就是当前楼层: 17 - 总计耗时 += 5; 18 - 设置读入的目标楼层为当前楼层 19 205. 输出总计耗时 21 22# 伪代码描述 231. get input: request_amount 242. init recorders: 25 - current_layer = 0 26 - total_time_cost = 0 273. for each request: 28 - get input: target_layer 29 - if target_layer < current_layer: 30 - total_time_cost += (current_layer - target_layer) * 4; 31 - elif target_layer > current_layer: 32 - total_time_cost += (target_layer - current_layer) * 6; 33 - total_time_cost += 5; 34 - current_layer = target_layer; 354. print(total_time_cost) 36 37*/ 38 39# include<iostream> 40using namespace std; 41 42int main(){ 43 int request_amount; 44 cin >> request_amount; 45 int current_layer = 0; 46 int total_time_cost = 0; 47 for (int i = 0; i < request_amount; i++){ 48 int target_layer; 49 cin >> target_layer; 50 if (target_layer > current_layer){ 51 total_time_cost += (target_layer - current_layer) * 6; 52 } else if (target_layer < current_layer) { 53 total_time_cost += (current_layer - target_layer) * 4; 54 } 55 total_time_cost += 5; 56 current_layer = target_layer; 57 } 58 59 cout << total_time_cost; 60 return 0; 61} 62
Mastodon