题解 | #成绩排序#

成绩排序

http://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

//定义学生结构体
struct Student {
	int number;
	int score;
	Student() {}
	Student(int n,int s):number(n),score(s) {}
};

//定义数组保存比较学生的基本信息
Student arr[100];

//定义比较函数
bool Compare(Student s1,Student s2) {
	//成绩相同比学号
	if(s1.score == s2.score) {
		return s1.number < s2.number;   //'<',指按照比较的参数由小到大排序
	}else{
		return s1.score < s2.score;     ////'<',指按照比较的参数由小到大排序,同理,如果是'>',指按照由大到小排序
	}
}

int main() {
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; ++i) {
		scanf("%d%d",&arr[i].number,&arr[i].score);
	}
	sort(arr,arr + n,Compare);
	for(int i = 0; i < n; ++i) {
		printf("%d %d\n",arr[i].number,arr[i].score);
	}
	return 0;
}
全部评论

相关推荐

头像
04-17 09:29
已编辑
湖南农业大学 后端
睡姿决定发型丫:本硕末9也是0offer,简历挂了挺多,只有淘天 美团 中兴给了面试机会,淘天二面挂,美团一面kpi面,中兴一面感觉也大概率kpi(虽然国企,但一面0技术纯聊天有点离谱吧)
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务