「PAT乙级真题解析」Basic Level 1098 岩洞施工 (问题分析+完整步骤+伪代码描述+提交通过代码)

题设给定了岩洞中每一个位置的顶部高度和底部高度, 要求判断是否能够将一个单位的长管道水平送入岩洞中。 由于是水平送入, 所以只需要判断顶部的最低位置和底部的最高位置之间的距离是否大于长管道的宽度。 由于题设给定长管道为1个单位宽, 所以我们只需要求出顶部的最低位置, 底部的最高位置, 检查二者差是否大于1即可。

#算法#数据结构#pat考试#c语言#需求分析

Table of Contents

乙级的题目训练主要用来熟悉编程语言的语法和形成良好的编码习惯和编码规范。从小白开始逐步掌握用编程解决问题。

PAT (Basic Level) Practice 1098 岩洞施工

问题分析

题设给定了岩洞中每一个位置的顶部高度和底部高度, 要求判断是否能够将一个单位的长管道水平送入岩洞中。 由于是水平送入, 所以只需要判断顶部的最低位置和底部的最高位置之间的距离是否大于长管道的宽度。 由于题设给定长管道为1个单位宽, 所以我们只需要求出顶部的最低位置, 底部的最高位置, 检查二者差是否大于1即可。

完整步骤描述

  1. 获取输入: 样本点个数, 各个样本点的顶部位置和底部位置
  2. 初始化记录器:
    • 顶部最低位置 = 1000;
    • 底部最高位置 = 0
  3. 对于每一个采样点:
    • 如果顶部位置低于当前顶部最低位置:
      • 顶部最低位置 = 当前顶部位置
    • 如果底部位置高于当前底部最高位置:
      • 底部最高位置 = 当前底部位置
  4. 计算 高度差 = 顶部最高位置 - 底部最高位置
  5. 如果 高度差 >= 1:
    • 输出"Yes"
    • 输出高度差
  6. 否则:
    • 输出"No"
    • 输出1-高度差

伪代码描述

  1. get input: sample_amount
  2. init recorder:
    • lowest_top = 1000
    • highest_bottom = 0
  3. for each sample:
    • get input: top of sample:
      • if top < lowest_top:
        • lowest_top = top
  4. for each sample:
    • get input: bottom of sample:
      • if bottom > highest_bottom:
        • highest_bottom = bottom
  5. calculate height = lowest_top - highest_bottom
  6. check:
    • if height >= 1:
      • print("Yes {height}")
    • else:
      • print("No {1 - height}")

完整提交代码

/*
# 问题分析
题设给定了岩洞中每一个位置的顶部高度和底部高度, 要求判断是否能够将一个单位的长管道水平送入岩洞中。
由于是水平送入, 所以只需要判断顶部的最低位置和底部的最高位置之间的距离是否大于长管道的宽度。
由于题设给定长管道为1个单位宽, 所以我们只需要求出顶部的最低位置, 底部的最高位置, 检查二者差是否大于1即可。
 
# 完整步骤描述
1. 获取输入: 样本点个数, 各个样本点的顶部位置和底部位置
2. 初始化记录器:
    - 顶部最低位置 = 1000;
    - 底部最高位置 = 0
3. 对于每一个采样点:
    - 如果顶部位置低于当前顶部最低位置:
        - 顶部最低位置 = 当前顶部位置
    - 如果底部位置高于当前底部最高位置:
        - 底部最高位置 = 当前底部位置
4. 计算 高度差 = 顶部最高位置 - 底部最高位置
5. 如果 高度差 >= 1:
    - 输出"Yes"
    - 输出高度差
6. 否则:
    - 输出"No"
    - 输出1-高度差
 
# 伪代码描述
1. get input: sample_amount
2. init recorder:
    - lowest_top = 1000
    - highest_bottom = 0
3. for each sample:
    - get input: top of sample:
        - if top < lowest_top:
            - lowest_top = top
4. for each sample:
    - get input: bottom of sample:
        - if bottom > highest_bottom:
            - highest_bottom = bottom
5. calculate height = lowest_top - highest_bottom
6. check:
    - if height >= 1:
        - print("Yes {height}")
    - else:
        - print("No {1 - height}")
*/
 
 
# include<stdio.h>
 
int main(){
    int sample_amount;
    scanf("%d", &sample_amount);
 
    int top[sample_amount];
    int bottom[sample_amount];
    int lowest_top = 1000;
    for (int i = 0; i < sample_amount; i++){
        scanf("%d", &top[i]);
        if (top[i] < lowest_top){
            lowest_top = top[i];
        }
    }
 
    int highest_bottom = 0;
    for (int i = 0; i < sample_amount; i++){
        scanf("%d", &bottom[i]);
        if (bottom[i] > highest_bottom){
            highest_bottom = bottom[i];
        }
    }
 
    int height = lowest_top - highest_bottom;
    if (height >= 1){
        printf("Yes %d", height);
    } else {
        printf("No %d", 1 - height);
    }
 
    return 0;
}
「PAT乙级真题解析」Basic Level 1098 岩洞施工 (问题分析+完整步骤+伪代码描述+提交通过代码) | 生活糖果