PAT 1036 Boys vs Girls

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 namegenderID 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​F​​−grade​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

 

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Person{
    string name;
    string gender;
    string id;
    int grade;
};
bool cmp(Person* p1,Person* p2){
    return p1->grade<p2->grade;
}
int main()
{
    int N;
    cin>>N;
    vector<Person*> vector1;
    vector<Person*> vector2;
    for(int i=0;i<N;i++){
        Person* p=new Person();
        cin>>p->name>>p->gender>>p->id>>p->grade;
        if(p->gender=="M"){
            vector1.push_back(p);
        }else{
            vector2.push_back(p);
        }
    }
    sort(vector1.begin(),vector1.end(),cmp);
    sort(vector2.begin(),vector2.end(),cmp);
    bool flag=false;
    Person* p1,*p2;
    if(vector2.size()==0){
        cout<<"Absent"<<endl;
        flag=true;
    }else{
        int s=vector2.size();
        p2=vector2[s-1];
        cout<<p2->name<<" "<<p2->id<<endl;
    }
    if(vector1.size()==0){
        cout<<"Absent"<<endl;
        flag=true;
    }else{
        p1=vector1[0];
        cout<<p1->name<<" "<<p1->id<<endl;
    }
    if(flag){
        cout<<"NA"<<endl;
    }else{
        cout<<p2->grade-p1->grade<<endl;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章