成绩排名

读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

备注:本题出自http://pat.zju.edu.cn/contests/pat-b-practise/1004

分析:这是一道简单的排序题,排序的对象为学生,排序的判别准则为学生的成绩。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Student
{
	string m_name;
	string m_number;
	int m_score;
	friend istream& operator>>(istream& is,Student& stu);
	friend ostream& operator<<(ostream& os,Student& stu);
};

istream& operator>>(istream& is,Student& stu)
{
	is>>stu.m_name>>stu.m_number>>stu.m_score;
	return is;
}

ostream& operator<<(ostream& os,Student& stu)
{
	os<<stu.m_name<<" "<<stu.m_number;
	return os;
}

bool score_compare(const Student& lhs,const Student& rhs)
{
	return lhs.m_score<rhs.m_score;
}

int main()
{
	int student_number;
	cin>>student_number;
  	if (student_number<=0)
            return -1;
	vector<Student> stu(student_number);
	for(int i=0;i<student_number;++i)
		cin>>stu[i];
	sort(stu.begin(),stu.end(),score_compare);
	cout<<stu[student_number-1]<<endl;
  	cout<<stu[0]<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值