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

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

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}")
*/# includeint 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;
}

相关内容

热门资讯

最新或2023(历届)江西财经... 江西财经大学现代经济管理学院是一所以经济、管理类学科为主,法、工、文等学科协调发展的本科院校。学院是...
最新或2023(历届)江西科技... 江西科技师范大学理工学院(国标代码:13440)是由江西科技师范大学申办并于2001年报经省教育厅和...
最新或2023(历届)赣南师范... 赣南师范大学科技学院(以下简称:学院)是2003年经江西省教育厅、江西省发计委批准,国家教育部确认,...
漫步祁连雾凇童话世界 连日来,祁连山腹地气温持续走低,独特的高原河谷地貌造就了壮观的雾凇奇观。我省海北藏族自治州祁连县八宝...
小区物业好粥到 物业工作人员为小区业主送八宝粥。西海新闻记者 燕卓 摄本报讯(西海新闻记者 燕卓)1月23日,西宁市...