题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include <stdio.h>
#include <stdlib.h>
#define maxn 100
typedef struct student{
	char name[maxn];
	int sorce;
}student; 
int compare0(const void *a,const void *b){
	const struct student *pa=(const struct student *)a;
	const struct student *pb=(const struct student *)b;
	if(pa->sorce>=pb->sorce) return -1;
	else return 1;
}
int compare1(const void *a,const void *b){
	const struct student *pa=(const struct student *)a;
	const struct student *pb=(const struct student *)b;
	if(pb->sorce>=pa->sorce) return -1;
	else return 1;
}
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		int temp;
		scanf("%d",&temp);
		student s[n];
		for(int i=0;i<n;i++){
			scanf("%s %d",&s[i].name,&s[i].sorce);
		}
		if(temp==0)
				qsort(s,n,sizeof(struct student),compare0);
	    else if(temp==1)
				qsort(s,n,sizeof(struct student),compare1);
		for(int i=0;i<n;i++){
			printf("%s %d\n",s[i].name,s[i].sorce);
		}
	}
	return 0;
} 

全部评论

相关推荐

03-12 15:35
嘉应学院 Python
M_地球online...:真“boss直聘”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务