信息学奥赛一本通 1178:成绩排序 | OpenJudge NOI 1.10 03:成绩排序

【题目链接】

ybt 1178:成绩排序
OpenJudge NOI 1.10 03:成绩排序

【题目考点】

1. 结构体 排序

【君义精讲】排序算法

2. 多关键字排序

方法1:将多关键字的排序条件整合为单一排序条件
方法2:使用稳定的排序算法进行多趟排序

【解题思路】

对结构体对象排序,比较的是成员变量,交换的是对象

【题解代码】

解法1:将多关键字的排序条件整合为单一排序条件

整合成的排序条件为:分数更高,或分数相同且名字字典序小,则排在前面。

  • 冒泡排序
#include<bits/stdc++.h>
using namespace std;
struct Stu
{
    char name[25];
    int score;
};
bool isPrior(Stu &a, Stu &b)//a是否应该排在b的前面 
{
    return a.score > b.score || a.score == b.score && strcmp(a.name, b.name) < 0;
}
int main()
{
    Stu stu[25];
	int n;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> stu[i].name >> stu[i].score;
	for(int i = 1; i <= n-1; ++i)//冒泡排序 
	   for(int j = 1; j <= n-i; ++j)
	       if(isPrior(stu[j+1], stu[j]))//如果stu[j+1]应该在stu[j]前面
                swap(stu[j], stu[j+1]); 
    for(int i = 1; i <= n; ++i)
		cout << stu[i].name << ' ' << stu[i].score << endl;
	return 0;
}
  • 插入排序
#include<bits/stdc++.h>
using namespace std;
struct Stu
{
    char name[25];
    int score;
};
bool isPrior(Stu &a, Stu &b)//a是否应该排在b的前面 
{
    return a.score > b.score || a.score == b.score && strcmp(a.name, b.name) < 0;
}
int main()
{
    Stu stu[25];
	int n;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> stu[i].name >> stu[i].score;
	for(int i = 2; i <= n; ++i)//插入排序
    {
        for(int j = i; j > 1; --j)
        {
            if(isPrior(stu[j], stu[j-1]))//如果stu[j+1]应该在stu[j]前面
                swap(stu[j], stu[j-1]);
            else
                break; 
        }
    }
    for(int i = 1; i <= n; ++i)
		cout << stu[i].name << ' ' << stu[i].score << endl;
	return 0;
}
  • STL sort函数排序
#include<bits/stdc++.h>
using namespace std;
struct Stu
{
    char name[25];
    int score;
};
bool cmp(Stu a, Stu b)//a是否应该排在b的前面 
{
    return a.score > b.score || a.score == b.score && strcmp(a.name, b.name) < 0;
}
int main()
{
    Stu stu[25];
	int n;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> stu[i].name >> stu[i].score;
	sort(stu+1, stu+1+n, cmp);	 
    for(int i = 1; i <= n; ++i)
		cout << stu[i].name << ' ' << stu[i].score << endl;
	return 0;
}
解法2:使用稳定的排序算法进行多趟排序

采用稳定的排序算法,先按名字字典序从小到大排序,再按成绩由高到低进行排序。
可以采用冒牌、插入,归并等稳定的排序算法。这里直接使用stable_sort进行排序。

#include<bits/stdc++.h>
using namespace std;
struct Stu
{
    char name[25];
    int score;
};
bool cmp1(Stu a, Stu b)//名字字典序小的排在前面 
{
    return strcmp(a.name, b.name) < 0;
}
bool cmp2(Stu a, Stu b)//分数高的排在前面 
{
    return a.score > b.score;
}
int main()
{
    Stu stu[25];
	int n;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> stu[i].name >> stu[i].score;
	stable_sort(stu+1, stu+1+n, cmp1);
	stable_sort(stu+1, stu+1+n, cmp2);
    for(int i = 1; i <= n; ++i)
		cout << stu[i].name << ' ' << stu[i].score << endl;
	return 0;
}
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值