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

PTA -L1-005 考试座位号

#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/169687.html

相关文章:

  • Spark-Streaming3
  • Flutter Dart新特性NulI safety late 关键字、空类型声明符?、非空断言!、required 关键字
  • 跨域问题(Cross-Origin Problem)
  • 第二次作业
  • 使用 NServiceBus 在 .NET 中构建分布式系统
  • python文本合并脚本
  • Transformer四模型回归打包(内含NRBO-Transformer-GRU、Transformer-GRU、Transformer、GRU模型)
  • RabbitMQ应用(基于腾讯云)
  • 第十二章-PHP文件上传
  • 缺省处理、容错处理
  • 使用 OpenCV 和 dlib 进行人脸检测
  • 使用 Vue 3 开发桌面端应用的框架性能对比
  • golang goroutine(协程)和 channel(管道) 案例解析
  • 【Java】jdk动态代理
  • Flink02-学习-套接字分词
  • Web前端开发:CSS Float(浮动)与 Positioning(定位)
  • 数据结构——二叉树和堆(万字,最详细)
  • 【AI论文】RefVNLI:迈向可扩展的主题驱动文本到图像生成评估
  • SLAM技术:从原理到应用的全面解析
  • 计算机网络 | 应用层(6) -- 套接字编程
  • Java自定义注解详解
  • 「Mac畅玩AIGC与多模态01」架构篇01 - 展示层到硬件层的架构总览
  • 深度学习常见框架:TensorFlow 与 PyTorch 简介与对比
  • 在 Ubuntu 24.04 系统上安装和管理 Nginx
  • body Param Query 三个 不同的入参 分别是什么意思 在前端 要怎么传 这三种不同的参数
  • DAY7-C++进阶学习
  • Python爬虫课程实验指导书
  • 麒麟系统搭建离线NTP服务器,让局域网内windows系统同步其时间,并付排错避坑思路
  • Android Studio中创建第一个Flutter项目
  • 前端性能优化面试回答技巧