PAT-1027 1028. List Sorting (25)

题目大意:提供用户个数 N 及 列号 M,提供 N 个用户的编号、姓名、成绩(各为一列,编号唯一),对 M 列进行排序,如果姓名或成绩列相同则按编号从小到大排列。

解题思路:题目的意思和逻辑很简单,不外乎排序 O(nlogn) ,但我的程序最后一组却显示超时,感觉没地儿优化了,难不成来个 基数排序 啊。上网搜集相关优化时发现,这道题卡的竟然是C++的 cout 、cin 效率 较之 C 的 printf、scanf 低。

题目链接:https://www.patest.cn/contests/pat-a-practise/1028

#include <iostream>              
#include <algorithm>              
#include <set>              
#include <map>              
#include <vector>              
#include <stack>              
#include <queue>              
#include <cmath>   
#include <cstring>           
using namespace std;

typedef struct Stu
{
	char name[9];
	int grade,num;
}Stu;
Stu stu[100005];

bool cmp1(Stu x1,Stu x2)
{
	if(x1.num < x2.num)
		return true;
	return false;
}

bool cmp2(Stu x1,Stu x2)
{
	if(strcmp(x1.name,x2.name) < 0)
		return true;
	else if(strcmp(x1.name,x2.name) == 0)
		if(x1.num < x2.num)
			return true;
	return false;
}

bool cmp3(Stu x1,Stu x2)
{
	if(x1.grade < x2.grade)
		return true;
	else if(x1.grade == x2.grade)
		if(x1.num < x2.num)
			return true;
	return false;
}

int main(int argc, char** argv) {
	int n,k;
	cin >> n >> k;
	for(int i=0;i<n;++i)
		scanf("%d %s %d",&stu[i].num,stu[i].name,&stu[i].grade);
	switch(k)
	{
		case 1:sort(stu,stu+n,cmp1);break;
		case 2:sort(stu,stu+n,cmp2);break;
		case 3:sort(stu,stu+n,cmp3);break;
	}	
	for(int i=0;i<n;++i)
		printf("%06d %s %d\n",stu[i].num,stu[i].name,stu[i].grade);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值