题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/7a2f7d304d9e43b1bb2a6e72ed65bf51

#include<iostream>
#include<string.h>

using namespace std;

typedef struct student {
    string name;
    int age;
    int score;
} student;

int main() {
    //输入
    int n;
    cin >> n;
    student* s = new student[n];
    for (int i = 0; i < n; i++)
        cin >> s[i].name >> s[i].age >> s[i].score;

    //一、按照年龄进行排序
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (s[j].age > s[j + 1].age)swap(s[j], s[j + 1]);
    //二、按照姓名进行排序
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (s[j].name > s[j + 1].name)swap(s[j], s[j + 1]);
    //三、按照分数进行排序  由低到高
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (s[j].score > s[j + 1].score)swap(s[j], s[j + 1]);

    //输出
    for (int i = 0; i < n; i++)
        cout << s[i].name << " " << s[i].age << " " << s[i].score << endl;

    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务