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

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.mrgr.cn/news/100460.html

相关文章:

  • 数据结构强化篇
  • 【文心快码】确实有点东西!
  • 【Maven】特殊pom.xml配置文件 - BOM
  • uniapp: 低功耗蓝牙(BLE)的使用
  • 前端Vue项目处理跨域请求问题解决方案(后端未加cors),前端调后端
  • Day23-Web开发——Linux
  • Java安全之cc链学习集合
  • Win11 配置 Git 绑定 Github 账号的方法与问题汇总
  • 【Spring Boot】Maven中引入 springboot 相关依赖的方式
  • C#本地使用离线ocr库识别图片中文本,工具包PaddleOCRSharp
  • pytorch学习使用
  • Pycharm(十七)生成器
  • 常用的性能提升手段--提纲
  • 【玩转 JS 函数式编程_016】DIY 实战:巧用延续传递风格(CPS)重构倒计时特效逻辑
  • 手动实现legend 与 echarts图交互 通过元素和js事件实现图标某项的高亮 显示与隐藏
  • Android源码编译命令详解
  • 深入理解布隆过滤器:参数设定与优化
  • 论文导读 - 基于大规模测量与多任务深度学习的电子鼻系统实现目标识别、浓度预测与状态判断
  • C++ ——引用
  • 大模型应用实战:深入理解模型上下文协议 MCP