藍橋杯 - 學生排序

這個題就是單純的結構體排序問題,有一個小問題就是,冒泡排序是穩定排序,是不會打亂相同大小數字的順序的,快速排序會打亂順序。
注意使用冒泡排序。

#include<iostream>
#include<algorithm>

using namespace std;

const int MAXN = 1005;

struct node {
    string name;
    string sex;
    int age;
    int score;
}st[MAXN];

int main() {
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> st[i].name >> st[i].sex >> st[i].age >> st[i].score;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n - i - 1; j++) {
            if(st[j].score > st[j+1].score) {
                swap(st[j], st[j+1]);
            }
        }
    }
    for(int i = 0; i < n; i++)
        cout << st[i].name << " " << st[i].sex << " "  << st[i].age << " "  << st[i].score << endl;
    return 0;
}
發佈了155 篇原創文章 · 獲贊 16 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章