1036 Boys vs Girls(排序)

1036 Boys vs Girls(排序)

0、题目

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student’s name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference grade**Fgrade**M. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

Sample Input 1:

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:

Mary EE990830
Joe Math990112
6

Sample Input 2:

1
Jean M AA980920 60

Sample Output 2:

Absent
Jean AA980920
NA

1、大致题意

给出 N 个同学的信息,输出女生中的最高分获得者的信息与男生中最低分获得者的信息,并输出他们的分数差。如果不存在女生或者男生,则对应获得者信息处输出Absent,而且差值处输出NA

2、基本思路

设计结构体Student,然后sort大法

struct Student {
	string name;
	string ID;
	int grade;
}

3、解题过程

3.1 operator> 的可疑问题(20/25)

首先第一份代码,写的很快,但是测试点2没过,很疑惑,感觉所有的点都考虑到了。

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

struct Student {
	string name;
	string ID;
	int grade;
	bool operator>(const Student a) const { //这行有问题
		if(grade!=a.grade) { //这行有问题
			if(ID!=a.ID) { //这行有问题
				return name>a.name; //这行有问题
			} else //这行有问题
				return ID>a.ID; //这行有问题
		} else { //这行有问题
			return grade>a.grade; //这行有问题
		}
	}
} tmp;

vector<Student>boys,girls;
int main() {
	int n;
	char sex;
	cin>>n;
	for(int i=0; i<n; i++) {
		cin>>tmp.name>>sex>>tmp.ID>>tmp.grade;
		if(sex=='M') {
			boys.push_back(tmp);
		} else if(sex=='F') {
			girls.push_back(tmp);
		}
	}
	sort(girls.begin(),girls.end(),greater<Student>());
	sort(boys.begin(),boys.end(),greater<Student>());
	if(!girls.empty()) {
		cout<<girls[0].name<<" "<<girls[0].ID<<endl;
	} else {
		cout<<"Absent"<<endl;
	}
	if(!boys.empty()) {
		cout<<boys[boys.size()-1].name<<" "<<boys[boys.size()-1].ID<<endl;
	} else {
		cout<<"Absent"<<endl;
	}
	if(!girls.empty()&&!boys.empty()) {
		int size=boys.size();
		int gg=girls[0].grade-boys[size-1].grade;
		cout<<gg;
	} else {
		cout<<"NA";
	}
	return 0;
}

在这里插入图片描述

然后经过一系列的代码比对

发现如果不用 operator> 的重写,用cmp可以过的很轻松

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

struct Student {
	string name;
	string ID;
	int grade;
	bool operator>(const Student a) const {
//		if(grade>a.grade){
//			return true;
//		}else{
//			return false;
//		}
		if(grade!=a.grade) { //这行有问题
			if(ID!=a.ID) { //这行有问题
				return name>a.name; //这行有问题
			} else //这行有问题 
				return ID>a.ID; //这行有问题
		} else { //这行有问题
			return grade>a.grade; //这行有问题
		}
	}
} tmp;

bool cmp1(Student s1,Student s2) {
	if(s1.grade<s2.grade) {
		return true;
	} else {
		return false;
	}
}

bool cmp2(Student s1,Student s2) {
	if(s1.grade>s2.grade) {
		return true;
	} else {
		return false;
	}
}

vector<Student>boys,girls;
int main() {
	int n;
	char sex;
	cin>>n;
	for(int i=0; i<n; i++) {
		cin>>tmp.name>>sex>>tmp.ID>>tmp.grade;
		if(sex=='M') {
			boys.push_back(tmp);
		} else if(sex=='F') {
			girls.push_back(tmp);
		}
	}
//	sort(girls.begin(),girls.end(),greater<Student>());
//	sort(boys.begin(),boys.end(),greater<Student>());
	sort(girls.begin(),girls.end(),cmp2);
	sort(boys.begin(),boys.end(),cmp1);
	if(!girls.empty()) {
		cout<<girls[0].name<<" "<<girls[0].ID<<endl;
	} else {
		cout<<"Absent"<<endl;
	}
	if(!boys.empty()) {
		cout<<boys[boys.size()-1].name<<" "<<boys[boys.size()-1].ID<<endl;
	} else {
		cout<<"Absent"<<endl;
	}
	if(!girls.empty()&&!boys.empty()) {
		int size=boys.size();
		int gg=girls[0].grade-boys[size-1].grade;
		cout<<gg;
	} else {
		cout<<"NA";
	}
	return 0;
}

在这里插入图片描述

直接AC,很奇怪,这两点有什么不同吗?经过排查发现下面的 operator> 就可以通过:

struct Student {
	string name;
	string ID;
	int grade;
	bool operator>(const Student a) const {
		if(grade>a.grade){
			return true;
		}else{
			return false;
		}
//		if(grade!=a.grade) { //这行有问题
//			if(ID!=a.ID) { //这行有问题
//				return name>a.name; //这行有问题
//			} else //这行有问题
//				return ID>a.ID; //这行有问题
//		} else { //这行有问题
//			return grade>a.grade; //这行有问题
//		}
	}
} ;

就是不能写成注释的样子。可是题目中明确说明It is guaranteed that all the grades are distinct.

最后发现,我的 operator> 写的确实有问题。

struct Student {
	string name;
	string ID;
	int grade;
	bool operator>(const Student a) const {
		if(grade==a.grade) { //注意这行
			if(ID==a.ID) {
				return ID>a.ID;
			} else
				return name>a.name;
		} else {
			if(grade>a.grade) {
				return true;
			} else {
				return false;
			}
		}
	}
} ;

3.2 AC代码

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

struct Student {
	string name;
	string ID;
	int grade;
	bool operator>(const Student a) const {
		if(grade==a.grade) {
			if(ID==a.ID) {
				return ID>a.ID;
			} else
				return name>a.name;
		} else {
			if(grade>a.grade) {
				return true;
			} else {
				return false;
			}
		}
	}
} tmp;

vector<Student>boys,girls;
int main() {
	int n;
	char sex;
	cin>>n;
	for(int i=0; i<n; i++) {
		cin>>tmp.name>>sex>>tmp.ID>>tmp.grade;
		if(sex=='M') {
			boys.push_back(tmp);
		} else if(sex=='F') {
			girls.push_back(tmp);
		}
	}
	sort(girls.begin(),girls.end(),greater<Student>());
	sort(boys.begin(),boys.end(),greater<Student>());
	if(!girls.empty()) {
		cout<<girls[0].name<<" "<<girls[0].ID<<endl;
	} else {
		cout<<"Absent"<<endl;
	}
	if(!boys.empty()) {
		cout<<boys[boys.size()-1].name<<" "<<boys[boys.size()-1].ID<<endl;
	} else {
		cout<<"Absent"<<endl;
	}
	if(!girls.empty()&&!boys.empty()) {
		int size=boys.size();
		int gg=girls[0].grade-boys[size-1].grade;
		cout<<gg;
	} else {
		cout<<"NA";
	}
	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值