牛客网 成绩排序(sort、清华机试)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunlanchang/article/details/88068099

题目描述

用一维数组存储学号和成绩,然后,按成绩排序输出。

输入描述:

输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。

输出描述:

按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。
示例1

输入

3
1 90
2 87
3 92

输出

2 87
1 90
3 92

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Stu
{
    int id, grade;
    bool operator<(const Stu &t) const
    {
        if (grade != t.grade)
            return grade < t.grade;
        return id < t.id;
    }
} stu[111];
int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; i++)
            scanf("%d%d", &stu[i].id, &stu[i].grade);
        sort(stu, stu + n);
        for (int i = 0; i < n; i++)
            printf("%d %d\n", stu[i].id, stu[i].grade);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunlanchang/article/details/88068099