pat 甲级A1028 List Sorting (25分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805468327690240

 

题目分析:

多混合排序问题。对不同键值进行排序。问题是写比较函数。注意:若将姓名定义成string类型超时。 不知道大家有没有出现这样的情况。望指正。string类型无法用printf以及scanf 输入或者输出。

 

参考代码:

#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
struct record{
	int score,id;
	char name[10];
}stu[100010];
int n,col;
bool cmp(record a, record b){
	if(col == 1) return a.id<b.id;
	else if(col == 2) return strcmp(a.name, b.name) != 0 ? strcmp(a.name, b.name) < 0 : a.id < b.id;
	else return a.score != b.score ? a.score < b.score : a.id<b.id;
}
int main(){
	scanf("%d %d", &n, &col);
	for(int i=0; i < n; i++)
		scanf("%d %s %d",&stu[i].id, stu[i].name, &stu[i].score);
	sort(stu, stu + n, cmp);
	for(int i = 0; i < n; i++){
		printf("%06d %s %d\n",stu[i].id, stu[i].name, stu[i].score);
    }
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值