前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >西安交通大学915-2018-编程2

西安交通大学915-2018-编程2

作者头像
lexingsen
发布2022-02-24 19:52:25
1200
发布2022-02-24 19:52:25
举报
文章被收录于专栏:乐行僧的博客乐行僧的博客

题目描述:

在这里插入图片描述
在这里插入图片描述

解题思路:

  • 选择一种稳定的排序算法,针对成绩关键字进行排序
  • 抽象学生类

代码实现:

代码语言:javascript
复制
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

struct Student {
    std::string name;
    int score;
};

namespace lsc {
    void swap(Student* arr, int i, int j) {
        Student t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
}

const int N = 100;
Student stu[N];
int idx = 0; // stu的当前下标

// 选择一种稳定的排序算法实现,  冒泡 归并 插入
void BubbleSort(Student* stu, int n) {
    int i = 0;
    int j = 0;
    bool flag = false;
    for (i = 0; i < n - 1; i++) {
        flag = true;
        for (j = i + 1; j < n - 1 - i; j++) {
            if (stu[i].score < stu[j].score) {
                lsc::swap(stu, i, j);
                flag = false;
            }
        }
        if (flag) {
            break;
        } 
    }
}

int main() {
    idx = 4;
    stu[0] = {"Jack", 70};
    stu[1] = {"Petter", 96};
    stu[2] = {"Joy", 70};
    stu[3] = {"LiLi", 89};
    auto f = [] (Student s) {cout << s.name << ":" << s.score << endl;};
    for_each(stu, stu + idx, f);
    BubbleSort(stu, idx);
    cout << endl;
    for_each(stu, stu + idx, f);
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 解题思路:
  • 代码实现:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档