C++代码 学生宿舍管理系统 ~顺序查找~ ~折半查找~ ~分块查找~ 完成按序号查询

时间: 2023-10-23 12:14:34 浏览: 42
以下是一个简单的学生宿舍管理系统的 C++ 代码,使用了顺序查找、折半查找和分块查找三种不同的查找方式,并且可以按照学生的序号进行查询。 ```c++ #include <iostream> #include <algorithm> using namespace std; const int MAXN = 1000; // 最大学生数 const int BLOCK_SIZE = 10; // 块大小 // 学生信息结构体 struct Student { int id; // 学号 string name; // 姓名 int dormitory; // 宿舍号 }; // 学生信息数组 Student students[MAXN]; int n; // 学生数 // 按学号从小到大排序 bool cmp(const Student& s1, const Student& s2) { return s1.id < s2.id; } // 顺序查找 int sequential_search(int id) { for (int i = 0; i < n; i++) { if (students[i].id == id) { return i; } } return -1; // 没有找到 } // 折半查找 int binary_search(int id) { int l = 0, r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (students[mid].id == id) { return mid; } else if (students[mid].id < id) { l = mid + 1; } else { r = mid - 1; } } return -1; // 没有找到 } // 分块查找 int block_search(int id) { // 先按学号从小到大排序 sort(students, students + n, cmp); // 计算块数 int num_blocks = (n + BLOCK_SIZE - 1) / BLOCK_SIZE; // 构建块信息数组 int block_data[num_blocks][2]; for (int i = 0; i < num_blocks; i++) { int l = i * BLOCK_SIZE; int r = min((i + 1) * BLOCK_SIZE, n) - 1; block_data[i][0] = students[l].id; block_data[i][1] = students[r].id; } // 在块信息数组中查找 int block_index = -1; for (int i = 0; i < num_blocks; i++) { if (block_data[i][0] <= id && id <= block_data[i][1]) { block_index = i; break; } } if (block_index == -1) { return -1; // 没有找到 } // 在块内顺序查找 int l = block_index * BLOCK_SIZE; int r = min((block_index + 1) * BLOCK_SIZE, n) - 1; for (int i = l; i <= r; i++) { if (students[i].id == id) { return i; } } return -1; // 没有找到 } int main() { // 读入学生信息 cin >> n; for (int i = 0; i < n; i++) { cin >> students[i].id >> students[i].name >> students[i].dormitory; } // 查询 int id_to_find; cin >> id_to_find; int result = block_search(id_to_find); if (result == -1) { cout << "Not found" << endl; } else { cout << students[result].name << " " << students[result].dormitory << endl; } return 0; } ``` 在以上代码中,我们定义了一个 `Student` 结构体来存储学生的信息,包括学号、姓名和宿舍号。我们还定义了三个查找函数 `sequential_search`、`binary_search` 和 `block_search`,分别使用了顺序查找、折半查找和分块查找三种不同的查找方式。在 `main` 函数中,我们读入学生信息,然后输入一个要查询的学号,最后调用 `block_search` 函数进行查询,如果找到了对应的学生,则输出其姓名和宿舍号,否则输出 "Not found"。 注意,在 `block_search` 函数中,我们先按学号从小到大排序,然后将学生信息分成若干个块,每个块的大小为 `BLOCK_SIZE`,然后在块信息数组中查找,最后在块内进行顺序查找。这种方法可以降低查找的时间复杂度,尤其是当数据量较大时。

相关推荐

最新推荐

recommend-type

C语言实现顺序表的顺序查找和折半查找

主要为大家详细介绍了C语言实现顺序表的顺序查找和折半查找,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

折半查找算法实现(C++).doc

折半查找法是数据结构与算法的应用中相对重要的一个查找方法。还可以通过数学方法计算其时间复杂度。
recommend-type

基于51单片机的音乐播放器设计+全部资料+详细文档(高分项目).zip

【资源说明】 基于51单片机的音乐播放器设计+全部资料+详细文档(高分项目).zip基于51单片机的音乐播放器设计+全部资料+详细文档(高分项目).zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

2024xxx市智能静态交通系统运营项目可行性实施方案[104页Word].docx

2024xxx市智能静态交通系统运营项目可行性实施方案[104页Word].docx
recommend-type

Cadence-Sigrity-PowerDC-2023.1版本的用户手册.pdf

Sigrity PowerDC technology provides comprehensive DC analysis for today's low voltage, high-current PCB and IC package designs. It is available with integrated thermal analysis to enable electrical and thermal co-simulation. Using PowerDC, you can assess critical end-to-end voltage margins for every device to ensure reliable power delivery. PowerDC quickly identifies areas of excess current density and thermal hotspots to minimize the risk of field failure in your design.
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。