[PAT-甲级]1036.Boys and Girls

来源:互联网 发布:python股票预测算法 编辑:程序博客网 时间:2024/06/08 07:46

1036. Boys vs Girls (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:

3Joe M Math990112 89Mike M CS991301 100Mary F EE990830 95
Sample Output 1:
Mary EE990830Joe Math9901126
Sample Input 2:
1Jean M AA980920 60
Sample Output 2:
AbsentJean AA980920NA



解题思路:结构数组排序,给定N个学生的信息,输出女生最高分与男生最低分的信息,并输出分值之间的差值。如果不存在男生或者女生则在对应出输出absent。同时差值为NA。对N个学生按照分数进行从大到小排序,然后去找对应的学生信息。


代码如下:

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;struct Student{string name;char s;string id;int grade;};bool cmp(const Student& s1, const Student& s2){return s1.grade > s2.grade;}vector<Student> students(110);int main(){  int stuNum;  cin>>stuNum;  for (int i = 0; i < stuNum; i++)      cin>>students[i].name>>students[i].s>>students[i].id>>students[i].grade;          sort(students.begin(), students.end(), cmp);      int male_grade, female_grade;   bool flag1 = true, flag2 = true;      for (int i = 0; i < students.size(); i++)  {      if (students[i].s == 'F')      {          cout<<students[i].name<<" "<<students[i].id<<endl;          flag1 = false;          female_grade = students[i].grade;          break;      }  }  if (flag1)      cout << "Absent"<<endl;          for (int i = students.size() - 1; i >= 0 ; i--)  {      if (students[i].s == 'M')      {          cout<<students[i].name<<" "<<students[i].id<<endl;          flag2 = false;          male_grade = students[i].grade;          break;      }  }  if (flag2)      cout << "Absent"<<endl;          if(!flag1 && !flag2)  cout<<female_grade-male_grade<<endl;  else  cout<<"NA"<<endl;  return 0;}




原创粉丝点击