[PAT-甲级]1036.Boys and Girls
来源:互联网 发布:python股票预测算法 编辑:程序博客网 时间:2024/06/08 07:46
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:
3Joe M Math990112 89Mike M CS991301 100Mary F EE990830 95Sample Output 1:
Mary EE990830Joe Math9901126Sample Input 2:
1Jean M AA980920 60Sample 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;}
- [PAT-甲级]1036.Boys and Girls
- PAT甲级.1036. Boys vs Girls (25)
- 1036. Boys vs Girls (25) PAT 甲级
- 【PAT甲级】1036. Boys vs Girls (25)
- PAT甲级1036. Boys vs Girls (25)
- PAT 甲级 1036. Boys vs Girls (25)
- PAT甲级 1036. Boys vs Girls (25)
- 1036. Boys vs Girls (25)-PAT甲级真题
- PAT(甲级)1036. Boys vs Girls (25)
- PAT甲级练习1036. Boys vs Girls (25)
- 【PAT】1036. Boys vs Girls
- PAT--1036. Boys vs Girls
- PAT甲级真题及训练集(10)--1036. Boys vs Girls (25)
- 【PAT甲级】1036. Boys vs Girls (25)——JAVA实现
- 1036. Boys vs Girls (25)-PAT
- 【PAT】1036. Boys vs Girls (25)
- PAT 1036. Boys vs Girls (25)
- PAT A 1036. Boys vs Girls (25)
- shell脚本学习第三天——特殊变量
- 三道NOIP(?)巧题
- 使用存储过程实现删除指定列名上存在的索引
- sublime部分插件安装详解
- hadoop完全分布式集群安装
- [PAT-甲级]1036.Boys and Girls
- python 笔记
- 数组中出现次数超过一半的数字
- 浅谈Three.js源码-render之WebGLCapabilities.js
- Hadoop集群完全分布式模式环境部署
- InteliJ IDEA
- 迷茫的新人
- CodeForces
- Linux