文章

34

粉丝

67

获赞

7

访问

9.1k

头像
成绩排序2.0 题解:
P1159 清华大学上机题
发布于2024年2月25日 15:39
阅读数 340

思想: 自定义 cmp 判断方法, 当 分数一样就 return p.index < q.index     // index 是学生编号

#include <bits/stdc++.h>

using namespace std;

struct student {
  int index;  
  int value;  
};

bool cmp(student x, student y) {
  if (x.value == y.value) {
    return x.index < y.index;
  } else {
    return x.value < y.value;
  }
}

int main() {
  int n;
  cin >> n;
  student p[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i].index >> p[i].value;
  }

  stable_sort(p, p + n, cmp);
  for (int i = 0; i < n; i++) {
    cout << p[i].index << " " << p[i].value << endl;
  }

  return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发