1036. Boys vs Girls (25)

来源:互联网 发布:淘宝客服挣钱吗 编辑:程序博客网 时间:2024/06/05 05:43

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

算法分析:本题比较简单,顺着题目的要求来做就行了,没有坑。就是选出一个成绩最好的女同学和一个成绩最差的男同学,然后再计算两者的得分差(女-男)。只是这里可能出现全是男或者全是女的情况。

#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct Student *students;struct Student // 这里不用加性别,用不上就不浪费空间了{    char name[11];    char id[11];    int score;};students init() //结构体的初始化{    int i;    students stu = malloc(sizeof(struct Student));    for(i = 0; i < 11; i++)    {        stu->id[i] = '\0';        stu->name[i] = '\0';    }    return stu;}int main(){    int i, n;    students male, female;    male = init();    female = init();    male->score = 101;    female->score = -1;    scanf("%d", &n);    for(i = 0; i < n; i++)    {        char name[11], gender[2], id[11];        int score;        scanf("%s %s %s %d", name, gender, id, &score);        if(gender[0] == 'F' && score > female->score) //找出成绩最好的女同学        {            strcpy(female->name, name);            strcpy(female->id, id);            female->score = score;        }        else if(gender[0] == 'M' && score < male->score) //找出成绩最好的男同学        {            strcpy(male->name, name);            strcpy(male->id, id);            male->score = score;        }    }    if(female->score > -1) //输出成绩最好的女同学        printf("%s %s\n", female->name, female->id);    else //没有女同学        printf("Absent\n");    if(male->score < 101) //输出成绩最好的男同学        printf("%s %s\n", male->name, male->id);    else //没有男同学        printf("Absent\n");    if(female->score > -1 && male->score < 101) //当男女都有时        printf("%d", female->score - male->score);    else //当只有男或者只有女时        printf("NA");    return 0;}
原创粉丝点击