1036 Boys vs Girls (25 分)(PAT甲级)(C语言)

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 grade​​ −grade​M. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

算法&代码

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#define Size 11
#define StuNum 101

struct SNode{
    char name[Size], ID[Size];
    int score, flag;
}Students[StuNum];

int comp(const void* a, const void* b)
{ 
    int k;
    if(((const struct SNode*)a)->flag > ((const struct SNode*)b)->flag) k = 1;
    else if(((const struct SNode*)a)->flag < ((const struct SNode*)b)->flag) k = -1;
    else{
        if(((const struct SNode*)a)->score > ((const struct SNode*)b)->score) k = 1;
        else k = -1;
    }
    return k;
}
int main()
{
    int n;
    char ch;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("\n%s %c", Students[i].name, &ch);
        if(ch == 'F') Students[i].flag = 1;  //1:girl;0:boy
        else Students[i].flag = 0;
        scanf("%s %d", Students[i].ID, &Students[i].score);
    }
    qsort(Students, n, sizeof(struct SNode), comp);
    if(Students[n-1].flag == 1&&Students[0].flag == 0){
        printf("%s %s\n", Students[n-1].name, Students[n-1].ID);
        printf("%s %s\n", Students[0].name, Students[0].ID);
        printf("%d\n", Students[n-1].score - Students[0].score);
    }else if(Students[n-1].flag != 1){
        printf("Absent\n");
        printf("%s %s\n", Students[0].name, Students[0].ID);
        printf("NA\n");
    }else{
        printf("%s %s\n", Students[n-1].name, Students[n-1].ID);
        printf("Absent\n");
        printf("NA\n");
    }
    return 0;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务