鱼C论坛

 找回密码
 立即注册
查看: 559|回复: 1

用选择排序法对4个班的成绩按每个同学的平均成绩的以非递增方式进行班内排序;

[复制链接]
发表于 2024-1-7 00:16:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
四个班的平均成绩保存在四个结构体数组中student students1[student_num];student students2[student_num];student students3[student_num];student students4[student_num];但未在班内排序排序,请改写代码并在主函数中实现

template<typename T>
struct Node {
    T data;
    Node* next;
    Node(T value) : data(value), next(nullptr) {}
};
template<typename T>
class LinkedList {
private:
    Node<T>* head;
    Node<T>* tail;

public:
    LinkedList() : head(nullptr), tail(nullptr) {}

    ~LinkedList() {
        while (head != nullptr) {
            Node<T>* temp = head;
            head = head->next;
            delete temp;
        }
    }
    void insert(T value) {
        Node<T>* newNode = new Node<T>(value);
        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    void display() const {
        Node<T>* current = head;
        while (current != nullptr) {
            std::cout << current->data << " -> ";
            current = current->next;
        }
        std::cout << "nullptr" << std::endl;
    }
};
void swap(double &a, double &b)
{
    if (a > b)
    {
        double temp = a;
        a =b;
        b = temp;
    }
}
LinkedList<int> list;
void makelist(student students[])
{
    double arr1[] = { 0 };
    double arr2[] = { 0 };
    double arr3[] = { 0 };
    double arr4[] = { 0 };

    double* begin1 = arr1;
    double* end1 = arr1 + sizeof(arr1) / sizeof(arr1[0]);
    double a = accumulate(begin1, end1, 0);
    double* begin2 = arr2;
    double* end2 = arr2 + sizeof(arr2) / sizeof(arr2[0]);
    double b = accumulate(begin2, end2, 0);
    double* begin3 = arr3;
    double* end3 = arr3 + sizeof(arr3) / sizeof(arr3[0]);
    double c = accumulate(begin3, end3, 0);
    double* begin4 = arr4;
    double* end4 = arr4 + sizeof(arr4) / sizeof(arr4[0]);
    double d = accumulate(begin4, end4, 0);

    swap(a, b);
    swap(b, c);
    swap(c, d);
    swap(a, b);
    swap(b, c);
    list.insert(a);
    list.insert(b);
    list.insert(c);
    list.insert(d);
    list.display();
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-7 00:16:37 | 显示全部楼层
首先,我们需要对每个班级的成绩按照平均成绩的非递增方式进行排序。你可以通过修改选择排序算法来实现这个目标。下面是修改后的代码:

  1. #include <iostream>

  2. struct student {
  3.     std::string name;
  4.     int score;
  5. };

  6. template<typename T>
  7. void selectionSort(T arr[], int n) {
  8.     for (int i = 0; i < n - 1; i++) {
  9.         int maxIndex = i;
  10.         for (int j = i + 1; j < n; j++) {
  11.             if (arr[j].score > arr[maxIndex].score) {
  12.                 maxIndex = j;
  13.             }
  14.         }
  15.         std::swap(arr[i], arr[maxIndex]);
  16.     }
  17. }

  18. int main() {
  19.     const int numStudents = 4;

  20.     // 假设已经初始化了四个班级的成绩
  21.     student students1[numStudents] = {{"Alice", 88}, {"Bob", 72}, {"Charlie", 95}, {"David", 82}};
  22.     student students2[numStudents] = {{"Eva", 78}, {"Frank", 69}, {"Grace", 91}, {"Henry", 85}};
  23.     student students3[numStudents] = {{"Ivy", 94}, {"Jack", 76}, {"Kelly", 81}, {"Leo", 87}};
  24.     student students4[numStudents] = {{"Mike", 90}, {"Nancy", 83}, {"Olivia", 79}, {"Peter", 92}};

  25.     // 对四个班级的成绩按照平均成绩进行排序
  26.     selectionSort(students1, numStudents);
  27.     selectionSort(students2, numStudents);
  28.     selectionSort(students3, numStudents);
  29.     selectionSort(students4, numStudents);

  30.     // 输出排序后的成绩
  31.     std::cout << "班级1:" << std::endl;
  32.     for (int i = 0; i < numStudents; i++) {
  33.         std::cout << students1[i].name << ":" << students1[i].score << std::endl;
  34.     }
  35.     std::cout << "班级2:" << std::endl;
  36.     for (int i = 0; i < numStudents; i++) {
  37.         std::cout << students2[i].name << ":" << students2[i].score << std::endl;
  38.     }
  39.     std::cout << "班级3:" << std::endl;
  40.     for (int i = 0; i < numStudents; i++) {
  41.         std::cout << students3[i].name << ":" << students3[i].score << std::endl;
  42.     }
  43.     std::cout << "班级4:" << std::endl;
  44.     for (int i = 0; i < numStudents; i++) {
  45.         std::cout << students4[i].name << ":" << students4[i].score << std::endl;
  46.     }

  47.     return 0;
  48. }
复制代码


这段代码通过选择排序算法对每个班级的学生成绩按照平均成绩的非递增方式进行了排序,并输出了排序后的结果。

注意,为了方便起见,我们假设每个班级都有4个学生,并且已经初始化了每个班级的成绩。你可以根据实际情况修改数组的大小和填充成绩数据。另外,你可能需要在代码中添加其他字段或修改结构体的定义来存储学生的其他信息。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 20:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表