文章

18

粉丝

181

获赞

53

访问

91.1k

头像
1151 成绩排序 清华上机 AC
P1151 清华大学上机题
发布于2022年7月17日 11:45
阅读数 6.6k

这题首先有两个坑:

  • 当成绩一样时,需要先输入的成绩排在前面。
    • 方法一:student结构体加入index字段,用来标识输入顺序(如下述代码)。
    • 方法二:用stable_sort代替sort,stable_sort在比较的两个值一致时不交换其下标位置
  • 这题需要用while(cin>>n)循环输入,否则会报WA,通过率只有66%。我看其他题解里面都没有使用。照他们提交的话是过不了的。

代码如下:


#include <bits/stdc++.h>

using namespace std;

struct student
{
    string name;
    int score;
    int index;
};

bool cmp1(student a, student b)
{
    if (a.score != b.score)
        return a.score < b.score;
    else
        return a.index < b.index;
}

bool cmp0(student a, student b)
{
    if (a.score != b.score)
        return a.score > b.score;
    else
        return a.index < b.index;
}

int n = 0;

int main()
{
    while (cin >> n)
    {           //人数
        int up; //1为升序,0为降序
        cin >> up;
        student s[n];
        for (int i = 0; i < n; i++)
        {
            cin >> s[i].name >> s[i].score;
            s[i].index = i;
        }
        ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发