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;
}
全部评论

相关推荐

不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务