pat甲级1036. Boys vs Girls (25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/richenyunqi/article/details/79579974

欢迎访问我的pat甲级题解目录哦https://blog.csdn.net/richenyunqi/article/details/79958195

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:

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++代码:

#include<bits/stdc++.h>
using namespace std;
struct Person{
    char name[11];
    char id[11];
    int score;
    Person(int s=0):score(s){}
};
int main(){
    int N;
    scanf("%d",&N);
    Person female(-1),male(101),temp;
    char gender;
    for(int i=0;i<N;++i){
        scanf("\n%s %c %s %d",&temp.name,&gender,&temp.id,&temp.score);
        if(gender=='F'&&temp.score>female.score)
                female=temp;
        else if(gender=='M'&&temp.score<male.score)
                male=temp;
    }
    if(female.score==-1)
        printf("Absent\n");
    else
        printf("%s %s\n",female.name,female.id);
    if(male.score==101)
        printf("Absent\n");
    else
        printf("%s %s\n",male.name,male.id);
    if(female.score!=-1&&male.score!=101)
        printf("%d",female.score-male.score);
    else
        printf("NA");
    return 0;
}


阅读更多

扫码向博主提问

日沉云起

用最短的代码,写最快的算法
  • 擅长领域:
  • 算法
去开通我的Chat快问
换一批

没有更多推荐了,返回首页