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

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

#include 
#include 
#include 
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);
	}
} 

你可能感兴趣的:(PTA数据结构与算法题目集,排序)