PAT甲级刷题记录——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, IDand grade, separated by a space, where nameand IDare strings of no more than 10 characters with no space, genderis either F(female) or M(male), and gradeis 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 gradeFgrade​M​​ . If one such kind of student is missing, output Absentin the corresponding line, and output NAin 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

思路

这题也很简单,这里提醒一下自己,能用string就用string,用char数组真的太麻烦了……赋值还得用strcpy()。

然后变量写的尽量清楚一点,我就是gender和grade这两个单词太像了,写第二个比较函数的时候直接用了Code::Blocks自动填充出来的东西,结果一直有一个测试点错误,检查了一下才发现居然第二个比较函数比较的是gender(惊了,这样也能过三个测试点……)。

题目本身很简单,就是一个排序题,把男生女生分开来排序,女生按成绩从高到低排,男生按成绩从低到高排,另外,不存在的情况只要通过男生人数和女生人数来判断就好了,如果男生人数或者女生人数为0,就输出Absent。最后一行的话,需要男生人数和女生人数都不为0,才会有结果,否则就输出NA。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1010;
struct student{
    string name;
    char gender;
    string ID;
    int grade;
}stu[maxn], stuM[maxn], stuF[maxn];
bool cmpF(student a, student b){
    return a.grade>b.grade;
}
bool cmpM(student a, student b){
    return a.grade<b.grade;
}
int main(){
    int N;
    cin>>N;
    int MaleNumber = 0, FemaleNumber = 0;
    for(int i=0;i<N;i++){
        cin>>stu[i].name>>stu[i].gender>>stu[i].ID>>stu[i].grade;
        if(stu[i].gender=='M'){
            stuM[MaleNumber].name = stu[i].name;
            stuM[MaleNumber].gender = stu[i].gender;
            stuM[MaleNumber].ID = stu[i].ID;
            stuM[MaleNumber].grade = stu[i].grade;
            MaleNumber++;
        }
        else{
            stuF[FemaleNumber].name = stu[i].name;
            stuF[FemaleNumber].gender = stu[i].gender;
            stuF[FemaleNumber].ID = stu[i].ID;
            stuF[FemaleNumber].grade = stu[i].grade;
            FemaleNumber++;
        }
    }
    if(FemaleNumber!=0){
        sort(stuF, stuF+FemaleNumber, cmpF);
        cout<<stuF[0].name<<" "<<stuF[0].ID<<'\n';
    }
    else printf("Absent\n");
    if(MaleNumber!=0){
        sort(stuM, stuM+MaleNumber, cmpM);
        cout<<stuM[0].name<<" "<<stuM[0].ID<<'\n';
    }
    else printf("Absent\n");
    if(FemaleNumber!=0&&MaleNumber!=0){
        cout<<stuF[0].grade-stuM[0].grade;
    }
    else printf("NA");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值