Boys VS Girls

问题描述

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.

输入格式

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.

输出格式

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

输入样例1

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
(结尾无空行)

输出样例1

Mary EE990830
Joe Math990112
6
(结尾无空行)

输入样例2

1
Jean M AA980920 60
(结尾无空行)

输出样例2

Absent
Jean AA980920
NA
(结尾无空行)

C语言代码

#include<stdio.h>
#include<string.h>

const int MAX = 15; //定义存放 name 和 ID 字符串的最大长度

//存储学生信息的结构体
typedef struct{
	char name[MAX];
	char gender;
	char ID[MAX];
	int grade;
}student;

//初始化结构体
void init(student* male_s, student* female_s){
	male_s->name [0]= female_s->name[0] = '\0';
	male_s->gender = 'M';
	female_s->gender = 'F';
	male_s->ID[0] = female_s->ID[0] = '\0';
	male_s->grade = 101; //男生选成绩最低的,因此初始化为 101
	female_s->grade = -1; //女生选成绩最高的,因此初始化为 -1
}

int main(){
	
	int n, grade;
	char name[MAX], gender, ID[MAX];
	student female , male; //记录女生中成绩最高的,和男生中成绩最低的
	init(&male , &female); //初始化男生和女生信息
	scanf("%d", &n);
	
	//循环输入学生信息,并找到题目要求的学生
	for(int i = 0; i < n; i++){ 
		scanf("%s %c %s %d", name, &gender, ID, &grade);
		if(gender == 'M' && grade < male.grade){ 
		//比较男生成绩,记录成绩最低的
			strcpy(male.name , name);
			strcpy(male.ID , ID);
			male.grade = grade;
		}
		if(gender == 'F' && grade > female.grade){
		//比较女生成绩,记录成绩最高的
			strcpy(female.name , name);
			strcpy(female.ID , ID);
			female.grade = grade;
		}
	}
	//按照规定要求输出信息
	if(female.grade == -1)
		printf("Absent\n");
	else
		printf("%s %s\n", female.name, female.ID);
		
	if(male.grade == 101)
		printf("Absent\n");
	else
		printf("%s %s\n", male.name, male.ID);
		
	if(female.grade != -1 && male.grade != 101)
		printf("%d", female.grade-male.grade);
	else
		printf("NA");
		
	return 0;
}

注意:

  1. 初始化结构体时,函数不能直接传入结构体,应该传入结构体的地址,才能返回得到初始化的结构,否则初始化是无效的
  2. 初始化时,男生、女生的成绩不能简单的初始化为0或者100,会影响后续对是否有此类学生的判断,根据学生成绩在 [0,100] 将其初始化为该范围外的值

Boys VS Girls 原题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值