当前位置: 首页 > web >正文

PTA -L1-005 考试座位号(BufferedReader、Arraylist动态数组、Map)

#PTA - L1-005 考试座位号
优化:
// 使用 BufferedReader:
// BufferedReader 提供了更快的输入速度,尤其是在处理大量数据时。
// 使用 br.readLine() 替代 sc.nextLine()。
// 使用 ArrayList 动态数组:
// ArrayList 可以动态调整大小,避免了固定数组大小限制的问题。
// 使用 Map 提高查询效率:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//        使用 BufferedReader:
//        BufferedReader 提供了更快的输入速度,尤其是在处理大量数据时。
//        使用 br.readLine() 替代 sc.nextLine()。
//        使用 ArrayList 动态数组:
//        ArrayList 可以动态调整大小,避免了固定数组大小限制的问题。
//        使用 Map 提高查询效率:
//        将座位号和考试信息的映射存储在 HashMap 中,查询的时间复杂度从 O(n) 降低到 O(1)。
//        避免直接访问字段:
//        使用 getter 方法访问类的私有字段,遵循了面向对象编程的封装原则。
//        简化查询逻辑:
//        通过 Map 直接获取查询结果,避免了嵌套循环。import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;class ExamCard {private String _ExamID;private int _tryseat;private int _examseat;public ExamCard() {}public ExamCard(String ExamID, int tryseat, int examseat) {this._ExamID = ExamID;this._tryseat = tryseat;this._examseat = examseat;}public String get_ExamID() {return _ExamID;}public void set_ExamID(String _ExamID) {this._ExamID = _ExamID;}public int get_tryseat() {return _tryseat;}public void set_tryseat(int _tryseat) {this._tryseat = _tryseat;}public int get_examseat() {return _examseat;}public void set_examseat(int _examseat) {this._examseat = _examseat;}
}public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// 使用 ArrayList 动态数组存储学生信息List<ExamCard> studentInfo = new ArrayList<ExamCard>();// 读取学生数量int student_Num = Integer.parseInt(br.readLine());// 读取每个学生的信息for (int i = 0; i < student_Num; i++) {String each_stu = br.readLine();String[] my_args = each_stu.split(" ");studentInfo.add(new ExamCard(my_args[0], Integer.parseInt(my_args[1]), Integer.parseInt(my_args[2])));}// 使用 Map 存储座位号和考试信息的映射,以提高查询效率Map<Integer, ExamCard> seatMap = new HashMap<>();for (ExamCard stu : studentInfo) {seatMap.put(stu.get_tryseat(), stu);}// 读取查询的学生数量int search_num = Integer.parseInt(br.readLine());// 读取要查询的每个idString str_ids[] = br.readLine().split(" ");// 查询并输出结果for (int i = 0; i < search_num; i++) {int id = Integer.parseInt(str_ids[i]);ExamCard stu = seatMap.get(id);if (stu != null) {System.out.println(stu.get_ExamID() + " " + stu.get_examseat());}}}
}
http://www.xdnf.cn/news/2607.html

相关文章:

  • 记录一下从debian12升级到debian13出现的小插曲01
  • C++翻转数相乘 2024年信息素养大赛复赛 C++小学/初中组 算法创意实践挑战赛 真题详细解析
  • shell编程基础知识及脚本示例
  • Android源码编译命令详解
  • 基于 Amazon RDS 数据库之间复制数据并屏蔽个人身份信息
  • 写读后感的时候,可以适当地引用书中的内容吗?
  • 【JavaGUI】
  • 边缘函数:全栈开发的最后1毫秒性能革命
  • MQTT - MQTT 实践(Windows EMQX、MQTTX、客户端认证、连接与主题)
  • ES练习册
  • Java多线程实现顺序执行
  • [计算机科学#3]:布尔逻辑 (计算机数学基础)
  • Python第三周作业
  • 语音合成之八-情感化语音合成的演进路线
  • [ECCV 2024]UMBRAE: Unified Multimodal Brain Decoding
  • 赞奇AIknow是什么?
  • 2025年8月PMP考试费用上涨?8月PMP考试费用解析!
  • 电力系统失步解列与振荡解析
  • 基于知识库的智能客户服务工具
  • Tailwind CSS 实战:基于 Kooboo 构建企业官网页面(二)
  • runtimeChunk的作用
  • Servlet (简单的servlet的hello world程序)
  • SAP-pp 怎么通过底表的手段查找BOM的全部ECN变更历史
  • 小红书笔记详情API接口概述及JSON数据返回参考
  • element通过业务按钮点击导入,调用el-upload的导入方法
  • Redis缓存问题的深度解析与解决方案
  • c++的匿名函数捕获
  • 代码小优化
  • Babel、core-js、Loader之间的关系和作用全解析
  • 人类社会的第四阶段