1036 Boys vs Girls (25 分)
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 gradeF−gradeM. 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
题目要求:
找出给出学生中,女生的最高分以及男生的最低分,求他们的差值。第一行输出女生的最高分学生的姓名和id,第二行输出男生的最低分学生的姓名和id,最后一行输出女生最高分与男生最低分的差值。如果学生全为男生,则女生对应信息的那一行输出Absent,最后 一行结果输出NA,若全为女生同。
解题思路:
两个数组分别存男生和女生,进行排序。
完整代码:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Student{
string name;
string ID;
int grade;
};
Student fstudent[1010];
Student mstudent[1010];
int fnum=0,mnum=0;
bool comp(Student student1,Student student2){
return student1.grade<student2.grade;
}
int main(){
int N;
int i;
string name,id;
char gender;
int grade;
cin>>N;
for(i=1;i<=N;i++){
cin>>name>>gender>>id>>grade;
if(gender == 'M'){
mstudent[mnum].name = name;
mstudent[mnum].ID = id;
mstudent[mnum].grade = grade;
mnum++;
}else{
fstudent[fnum].name = name;
fstudent[fnum].ID = id;
fstudent[fnum].grade = grade;
fnum++;
}
}
sort(mstudent,mstudent+mnum,comp);
sort(fstudent,fstudent+fnum,comp);
if(fnum == 0){
cout<<"Absent"<<endl;
}else{
cout<<fstudent[fnum-1].name<<" "<<fstudent[fnum-1].ID<<endl;
}
if(mnum == 0){
cout<<"Absent"<<endl;
}else{
cout<<mstudent[0].name<<" "<<mstudent[0].ID<<endl;;
}
if(fnum == 0 || mnum == 0){
cout<<"NA"<<endl;
}else{
cout<<fstudent[fnum-1].grade - mstudent[0].grade<<endl;
}
return 0;
}
代码简化
2019.1.25
#include<bits/stdc++.h>
using namespace std;
struct Stu{
string name;
string id;
int grade;
};
Stu mstu[10010],fstu[10010];
int mnum=0,fnum=0;
bool com(Stu s1,Stu s2){
return s1.grade>=s2.grade;
}
int main(){
int n,i;
string name,sex;
cin>>n;
for(i=0;i<n;i++){
cin>>name>>sex;
if(sex=="M"){
mstu[mnum].name=name;
cin>>mstu[mnum].id>>mstu[mnum].grade;
mnum++;
}else{
fstu[fnum].name=name;
cin>>fstu[fnum].id>>fstu[fnum].grade;
fnum++;
}
}
sort(mstu,mstu+mnum,com);
sort(fstu,fstu+fnum,com);
if(fnum==0) cout<<"Absent"<<endl;
else cout<<fstu[0].name<<" "<<fstu[0].id<<endl;
if(mnum==0) cout<<"Absent"<<endl;
else cout<<mstu[mnum-1].name<<" "<<mstu[mnum-1].id<<endl;
if(fnum!=0&&mnum!=0) cout<<fstu[0].grade - mstu[mnum-1].grade <<endl;
else cout<<"NA"<<endl;
return 0;
}