1028 List Sorting(排序)

这类题目归结于常用技巧与算法,有很鲜明的套路,重在理解其规则,通常写起来不算太复杂。

题目描述:
在这里插入图片描述
在这里插入图片描述
题目大致意思:
给出N个学生的学号,姓名和成绩,按其中的某个列,对其进行排序,在按姓名或者分数进行排列时,如果遇到了相同的数据,则按学号递增进行排序。
大致思路:
这道题相比前两道来说要简单很多,但核心思想是一样的。使用结构体来存储学生的学号,姓名和分数信息,进而用一个结构体数组来存储所有学生的信息。根据条件使用sort函数对结构体数组进行排序即可。
提交结果:
第七个测试用例差点就超过了时间限制。
在这里插入图片描述
提交代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
struct Student
{
	string id;
	string name;
	int score;
};
bool cmp1(Student stu1, Student stu2)
{
	return stu1.id < stu2.id;
}
bool cmp2(Student stu1, Student stu2)
{
	if (stu1.name != stu2.name)
		return stu1.name < stu2.name;
	else
	{
		return stu1.id < stu2.id;
	}
}
bool cmp3(Student stu1, Student stu2)
{
	if (stu1.score != stu2.score)
		return stu1.score < stu2.score;
	else
	{
		return stu1.id < stu2.id;
	}
}
vector<Student> arr;
int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		Student stu;
		cin >> stu.id >> stu.name >> stu.score;
		arr.push_back(stu);
	}
	if (m == 1)
	{
		sort(arr.begin(), arr.end(), cmp1);
		for (int i = 0; i < n; i++)
		{
			cout << arr[i].id << " " << arr[i].name << " " << arr[i].score << endl;
		}
	}
	if (m == 2)
	{
		sort(arr.begin(), arr.end(), cmp2);
		for (int i = 0; i < n; i++)
		{
			cout << arr[i].id << " " << arr[i].name << " " << arr[i].score << endl;
		}
	}
	if (m == 3)
	{
		sort(arr.begin(), arr.end(), cmp3);
		for (int i = 0; i < n; i++)
		{
			cout << arr[i].id << " " << arr[i].name << " " << arr[i].score << endl;
		}
	}
}

本次提交后累计得分523,排名为14290。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值