PTA数据结构与算法题目集7-37 模拟EXCEL排序(c语言实现)

原题链接
怎么说呢,这道题我偷懒了。
我必须要承认,我偷懒了。
但是还是必须要说stdlib.h库中的qsort函数是真的牛逼。
建议大家先百度这个函数再来看我的代码,这样会很好懂。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
	int xh;
	char xx[9];
	int cj;
}student;
int cmp1(const void *a,const void *b){
	student x=*(student *)a,y=*(student *)b;
	return x.xh>y.xh;
}
int cmp2(const void *a,const void *b){
	student x=*(student *)a,y=*(student *)b;
	if(strcmp(x.xx,y.xx)!=0){
		return strcmp(x.xx,y.xx);
	}else{
		return x.xh>y.xh;	
	}
}
int cmp3(const void *a,const void *b){
	student x=*(student *)a,y=*(student *)b;
	if(x.cj!=y.cj){
		return x.cj-y.cj;
	}else{
		return x.xh>y.xh;
	}
}
int main(){
	int n,c,i;
	scanf("%d %d",&n,&c);
	student p[n];
	for(i=0;i<n;i++){
		scanf("%d %s %d",&p[i].xh,p[i].xx,&p[i].cj);
	}
	if(c==1){
		qsort(p,n,sizeof(student),cmp1);
	}else if(c==2){
		qsort(p,n,sizeof(student),cmp2);
	}else{
		qsort(p,n,sizeof(student),cmp3);
	}
	for(i=0;i<n;i++){
		printf("%06d %s %d\n",p[i].xh,p[i].xx,p[i].cj);
	}
} 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值