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 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

题目大意:给出N个同学的信息,输出女生中的最高分获得者的信息与男生中最低分获得者的信息,并输出他们的分数差。如果不存在女生或者男生,则对应获得者信息处输出Absent,而且差值处输出NA

#include <iostream>
#include <string>
using namespace std;
/*
    给定N行 找出最高年级的 女学生 最低年级的 男学生
    其实这道题完全可以 按照 1006 那道题的思路来做 
    用M记录最低年级男生 F记录最高年级女生 tmp用来输入替换数据
    就不必像我这样这么麻烦 去开辟结构体数组 直接用变量替代即可
*/
struct Student{
    string name;
    string id;
    char gender;
    int grade; //所以这个grade到底是年级的意思 还是 成绩的意思 并不重要[doge] 应该是成绩吧 常理来说没有把年级排到100的吧哈哈
}stu[101];

int main(){
    int n;
    cin >> n;
    int highest = -1, lowest = 101; //分别保留最高年级 与 最低年级
    string female, fid; //保留最后要输出的 女生姓名和 id
    string male, mid; //保留最后要输出的 男生姓名和 id
    
    int flag1 = 0, flag2 = 0; //用来记录女生 和 男生 分别是否存在 0 代表不存在 1 代表存在
    //(其实也没有必要定义,定义了可能会更加清晰,因为如果highest与lowest的值未改变的话 就足以说明男生或者女生不存在 或者 都不存在)
    
    for(int i = 0; i < n; i++){
        cin >> stu[i].name >> stu[i].gender >> stu[i].id >> stu[i].grade;
        if(stu[i].gender == 'F'){
            flag1 = 1;
            if(stu[i].grade > highest){ //找到最高年级的女学生
                highest = stu[i].grade;
                female = stu[i].name;
                fid = stu[i].id;
            }
        }
        else{
            flag2 = 1;
            if(stu[i].grade < lowest){ //找到最低年级的男学生
                lowest = stu[i].grade;
                male = stu[i].name;
                mid = stu[i].id;
            }
        }
    }
    
    if(flag1 && flag2){
        cout << female << " " << fid << endl;
        cout << male << " " << mid << endl;
        cout << highest - lowest << endl;
    }
    else if(flag1){
        cout << female << " " << fid << endl;
        cout << "Absent" << endl;
        cout << "NA" << endl;
    }
    else{
        cout << "Absent" << endl;
        cout << male << " " << mid << endl;
        cout << "NA" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值