PAT A1036. Boys vs Girls (25)

  1. 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:

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
提交代碼
版權所有 (C) 2011-2018 浙江大學計算機科學與技術學院

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=10010;
struct Stu{
    char name[20];
    char sex;
    char id[20];
    int s;
}stu[maxn];
bool cmp1(Stu a,Stu b){
    if(a.sex!=b.sex)return a.sex<b.sex;
    else return a.s>b.s;
}
bool cmp2(Stu a,Stu b){
    if(a.sex!=b.sex)return a.sex>b.sex;
    else return a.s<b.s;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s %c %s %d",stu[i].name,&stu[i].sex,stu[i].id,&stu[i].s);
    }
    int mk=0;
    int s1=0,s2=0;
    sort(stu,stu+n,cmp1);
    if(stu[0].sex=='F'){printf("%s %s\n",stu[0].name,stu[0].id);s1=stu[0].s;}
    else {printf("Absent\n");mk=1;}
    sort(stu,stu+n,cmp2);
    if(stu[0].sex=='M'){printf("%s %s\n",stu[0].name,stu[0].id);s2=stu[0].s;}
    else {printf("Absent\n");mk=1;}
    if(mk==1)printf("NA");
    else printf("%d",s1-s2);
    return 0;
}

這裏寫圖片描述

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