机试准备(二)——排序

时间限制: 常见1s,代表百万级运算级。比如:估计可以知道,n>3000,则不能使用冒泡排序(O( N 2 N^2 N2))。
掌握算法: 冒泡排序、快排等。
STL库用法
例子:
链接:https://www.nowcoder.com/questionTerminal/bf3ec474bb7d410dbb9d5bbcd07a93e5
来源:牛客网

对每个测试用例,首先输出1行“Case:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
using namespace std;

#define N 100000

static int c;

struct stu {
	int id;
	string name;
	int score;
}student[N];

bool cmp(stu a, stu b) {
	if (c == 1) {
		return a.id < b.id;
	}
	else if (c == 2) {
		if (a.name != b.name) {
			return a.name < b.name;
		}
		else {
			return a.id < b.id;
		}
	}
	else {
		if (a.score != b.score) {
			return a.score < b.score;
		}
		else {
			return a.id < b.id;
		}
	}
}

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		cin >> c;
		for (int i = 0; i < n; i++) {
			cin >> student[i].id >> student[i].name >> student[i].score;
		}
		sort(student, student+n, cmp);
		cout << "Case:" << endl;
		for (int i = 0; i < n; i++) {
			cout << setw(6) << setfill('0') << student[i].id << " ";
			cout << student[i].name << " " << student[i].score << endl;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值