fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. #define MAX 100 // Giới hạn số học sinh tối đa
  7.  
  8. struct HocSinh {
  9. char hoTen[50];
  10. char gioiTinh[10];
  11. int namSinh;
  12. double diemTongKet;
  13. };
  14.  
  15. // Hàm nhập danh sách học sinh
  16. void nhapDanhSach(HocSinh ds[], int &n) {
  17. cout << "Nhập số lượng học sinh: ";
  18. cin >> n;
  19. cin.ignore(); // Xóa bộ đệm để nhập chuỗi
  20.  
  21. for (int i = 0; i < n; i++) {
  22. cout << "Nhập thông tin học sinh thứ " << i + 1 << ":\n";
  23. cout << "Họ và tên: ";
  24. cin.getline(ds[i].hoTen, 50);
  25. cout << "Giới tính: ";
  26. cin.getline(ds[i].gioiTinh, 10);
  27. cout << "Năm sinh: ";
  28. cin >> ds[i].namSinh;
  29. cout << "Điểm tổng kết: ";
  30. cin >> ds[i].diemTongKet;
  31. cin.ignore(); // Xóa bộ đệm để nhập chuỗi tiếp theo
  32. }
  33. }
  34.  
  35. // Hàm sắp xếp danh sách theo điểm giảm dần (Dùng thuật toán sắp xếp chèn - Insertion Sort)
  36. void sapXepDiemGiamDan(HocSinh ds[], int n) {
  37. for (int i = 1; i < n; i++) {
  38. HocSinh key = ds[i];
  39. int j = i - 1;
  40. while (j >= 0 && ds[j].diemTongKet < key.diemTongKet) {
  41. ds[j + 1] = ds[j];
  42. j--;
  43. }
  44. ds[j + 1] = key;
  45. }
  46. }
  47.  
  48. // Hàm tìm kiếm tuần tự theo tên
  49. void timKiemTuanTuTen(HocSinh ds[], int n, char ten[]) {
  50. bool found = false;
  51. for (int i = 0; i < n; i++) {
  52. if (strcmp(ds[i].hoTen, ten) == 0) {
  53. cout << "Tìm thấy: " << ds[i].hoTen << " - " << ds[i].diemTongKet << endl;
  54. found = true;
  55. }
  56. }
  57. if (!found) cout << "Không tìm thấy học sinh có tên: " << ten << endl;
  58. }
  59.  
  60. // Hàm tìm kiếm tuần tự theo điểm
  61. void timKiemTuanTuDiem(HocSinh ds[], int n, double diem) {
  62. bool found = false;
  63. for (int i = 0; i < n; i++) {
  64. if (ds[i].diemTongKet == diem) {
  65. cout << "Tìm thấy: " << ds[i].hoTen << " - " << ds[i].diemTongKet << endl;
  66. found = true;
  67. }
  68. }
  69. if (!found) cout << "Không tìm thấy học sinh có điểm: " << diem << endl;
  70. }
  71.  
  72. // Hàm tìm kiếm nhị phân theo điểm tổng kết (Cần danh sách đã sắp xếp)
  73. void timKiemNhiPhanDiem(HocSinh ds[], int n, double diem) {
  74. int left = 0, right = n - 1;
  75. while (left <= right) {
  76. int mid = left + (right - left) / 2;
  77. if (ds[mid].diemTongKet == diem) {
  78. cout << "Tìm thấy: " << ds[mid].hoTen << " - " << ds[mid].diemTongKet << endl;
  79. return;
  80. } else if (ds[mid].diemTongKet < diem) {
  81. right = mid - 1; // Vì danh sách giảm dần nên cần giảm right
  82. } else {
  83. left = mid + 1;
  84. }
  85. }
  86. cout << "Không tìm thấy học sinh có điểm: " << diem << endl;
  87. }
  88.  
  89. // Hàm hiển thị danh sách học sinh
  90. void hienThiDanhSach(HocSinh ds[], int n) {
  91. cout << "\nDanh sách học sinh:\n";
  92. for (int i = 0; i < n; i++) {
  93. cout << ds[i].hoTen << " - " << ds[i].gioiTinh << " - "
  94. << ds[i].namSinh << " - " << ds[i].diemTongKet << endl;
  95. }
  96. }
  97.  
  98. // Chương trình chính
  99. int main() {
  100. HocSinh ds[MAX];
  101. int n;
  102. nhapDanhSach(ds, n);
  103.  
  104. // Sắp xếp danh sách giảm dần theo điểm
  105. sapXepDiemGiamDan(ds, n);
  106.  
  107. // Hiển thị danh sách sau khi sắp xếp
  108. hienThiDanhSach(ds, n);
  109.  
  110. // Tìm kiếm tuần tự theo tên
  111. char ten[50];
  112. cout << "\nNhập tên học sinh cần tìm (tìm kiếm tuần tự): ";
  113. cin.getline(ten, 50);
  114. timKiemTuanTuTen(ds, n, ten);
  115.  
  116. // Tìm kiếm tuần tự theo điểm
  117. double diem;
  118. cout << "\nNhập điểm cần tìm (tìm kiếm tuần tự): ";
  119. cin >> diem;
  120. timKiemTuanTuDiem(ds, n, diem);
  121.  
  122. // Tìm kiếm nhị phân theo điểm
  123. cout << "\nNhập điểm cần tìm (tìm kiếm nhị phân): ";
  124. cin >> diem;
  125. timKiemNhiPhanDiem(ds, n, diem);
  126.  
  127. return 0;
  128. }
  129.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Nhập số lượng học sinh: 
Danh sách học sinh:

Nhập tên học sinh cần tìm (tìm kiếm tuần tự): Không tìm thấy học sinh có tên: 

Nhập điểm cần tìm (tìm kiếm tuần tự): Không tìm thấy học sinh có điểm: 0

Nhập điểm cần tìm (tìm kiếm nhị phân): Không tìm thấy học sinh có điểm: 0