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

相关推荐

机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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