文章

19

粉丝

51

获赞

3

访问

4.2k

头像
成绩排序 题解:C++ 不使用stable_sort的一种思路
P1151 清华大学上机题
发布于2024年3月9日 22:23
阅读数 240

#include <bits/stdc++.h>
using namespace std;
 struct student{
    char name[20];
    int grade;
    int seq;
} ;
bool comp1(student lhs,student rhs)
{
    if(lhs.grade<rhs.grade)
        return true;
    else if(lhs.grade==rhs.grade&&lhs.seq<rhs.seq)
        return true;
    else
        return false;
}
bool comp0(student lhs,student rhs)
{
    if(lhs.grade>rhs.grade)
        return true;
    else if(lhs.grade==rhs.grade&&lhs.seq<rhs.seq)
        return true;
    else
        return false;
}
int main()
{   student stu [1000];
    int num,i,n;
    while(scanf("%d%d",&i,&n)!=EOF){
    for (num=0;num<i;num++)
    {
        scanf("%s%d",&stu[num].name,&stu[num].grade);
        stu[num].seq=num;
    }
    if(n==0) sort(stu,stu+i,comp0);
    if(n==1) sort(stu,stu+i,comp1);
    for(int b=0;b<i;b++)
    {
        printf("%s %d\n",stu[b].name,stu[b].grade);
    }}
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发