文章

27

粉丝

0

获赞

80

访问

2.4k

头像
成绩排序 题解:自定义sort排序
P1151 清华大学上机题
发布于2026年2月24日 11:56
阅读数 34

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  struct student{
    string name;
    int score;
  };
  int num;
  int method;
  while(cin>>num>>method){
    vector<student> stu(num);
    for(int i=0;i<num;i++){
      cin>>stu[i].name>>stu[i].score;
    }
    // 按 method:1 升序,否则降序;lambda 需捕获 method
    stable_sort(stu.begin(),stu.end(),[method](const student &a,const student &b){
      return method==1 ? a.score<b.score : a.score>b.score;
    });
    for(student s:stu){
      cout<<s.name<<" "<<s.score<<"\n";
    }
  }
  return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发