IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> Boys VS Girls -> 正文阅读

[C++知识库]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 原题

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-13 11:45:37  更:2021-08-13 11:48:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 11:16:47-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码