目录

​1,题目描述​

​ 题目大意​

​2,思路​

​3,代码​


1,题目描述

PAT_甲级_1036 Boys vs Girls (25分) (C++)【签到题/结构体排序】_甲级

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

 题目大意

输出女生中成绩最高的、男生中成绩最低的人的名字和ID,以及他们两人的成绩差(题目保证无相同的两个成绩)。

 

2,思路

结构体排序。SHOW CODE

 

3,代码

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

struct stu{
string name, ID;
int grade;
};
bool cmp1(stu a, stu b){
return a.grade > b.grade;
}
int main(){
//#ifdef ONLINE_JUDGE
//#else
// freopen("1.txt", "r", stdin);
//#endif

int n;
cin>>n;

vector<stu> male, female;
string name, id;
char gender;
int grade;
for(int i = 0; i < n; i++){
cin>>name>>gender>>id>>grade;
if(gender == 'M')
male.push_back({name, id, grade});
else
female.push_back({name, id, grade});
}
sort(male.begin(), male.end(), cmp1);
sort(female.begin(), female.end(), cmp1);

int flag = 0;
if(female.size() == 0){
cout<<"Absent"<<endl;
flag = 1;
}else cout<< female[0].name <<' '<< female[0].ID << endl;

if(male.size() == 0){
cout<<"Absent"<<endl;
flag = 1;
}else cout<< male[male.size() - 1].name <<' '<< male[male.size() - 1].ID << endl;

if(flag == 1) cout<<"NA";
else cout<<female[0].grade - male[male.size()-1].grade;

return 0;
}