【PAT】1036 Boys vs Girls

1036 Boys vs Girls (25分)

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​F​​ −grade​M. 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

刷题地址

题意:

  • 输入N个学生的姓名、性别、ID、成绩。
  • 第一行输出成绩最高的女生(F)的姓名、ID。
  • 第二行输出成绩最低的男生(M)的姓名、ID。
  • 第三行 成绩最高的女生成绩 - 成绩最低的男生成绩。
  • 注:如果第一行或第二行没有符合条件的,输出Absent,且第三行输出NA。

思路:

  • 使用判断条件找出成绩最高的女生和成绩最低的男生。
  • 运用四个if语句对min和max的值进行检查,若初值没有变,说明没有找到对应的女生和男生,按题目意思给出的条件输出。

C++代码:

#include<iostream>
#include<cstring>	//使用了strcpy需导入 
using namespace std;

int main()
{
	int N;	//男生、女生的个数 
	char name[11];	//姓名 
	char ID[11];	//ID
	char gender;	//性别 
	int grade;		//成绩 
	char maxname[11];	//成绩最高的女生姓名 
	char maxID[11];		//成绩最高的女生ID 
	char minname[11];	//成绩最低的男生姓名 
	char minID[11];		//成绩最低的男生ID
	int max=-1;			//题目中所给成绩范围 0 ~ 100,此处 max 初始化为 -1 
	int min=101;		//题目中所给成绩范围 0 ~ 100,此处 min 初始化为 101
	scanf("%d",&N);		//输入男生、女生的个数 
	while(N-->0)		//等同于 for(i=0;i<N;i++) 
	{
		scanf("%s %c %s %d",name,&gender,ID,&grade);
		if(gender=='F')	//判断是否为女生,找出成绩最高者,实时刷新数值 
		{
			if(grade>max)
			{
				max=grade;
				strcpy(maxname,name);
				strcpy(maxID,ID);
			}
		} 
		else if(gender=='M')	//判断是否为男生,找出成绩最低者,实时刷新数值 
		{
			if(grade<min)
			{
				min=grade;
				strcpy(minname,name);
				strcpy(minID,ID);
			}
		}
	}
	if(max!=-1 && min!=101)	//能找到成绩最高的女生、能找到成绩最低的男生 
	{
		printf("%s %s\n",maxname,maxID);
		printf("%s %s\n",minname,minID);
		printf("%d\n",max-min);
	}
	if(max==-1 && min!=101)	//找不到成绩最高的女生、能找到成绩最低的男生 
	{
		printf("Absent\n",maxname,maxID);
		printf("%s %s\n",minname,minID);
		printf("NA\n",max-min);
	}
	if(max!=-1 && min==101)	//能找到成绩最高的女生、找不到成绩最低的男生 
	{
		printf("%s %s\n",maxname,maxID);
		printf("Absent\n",minname,minID);
		printf("NA\n",max-min);
	}
	if(max==-1 && min==101)	//找不到成绩最高的女生、找不到成绩最低的男生 
	{
		printf("Absent\n",maxname,maxID);
		printf("Absent\n",minname,minID);
		printf("NA\n",max-min);
	}
	return 0;
}

C语言代码:

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

int main()
{
	int N;	//男生、女生的个数 
	char name[11];	//姓名 
	char ID[11];	//ID
	char gender;	//性别 
	int grade;		//成绩 
	char maxname[11];	//成绩最高的女生姓名
	char maxID[11];	//成绩最高的女生ID 
	char minname[11];	//成绩最低的男生姓名
	char minID[11];	//成绩最低的男生ID
	int max=-1;	//题目中所给成绩范围 0 ~ 100,此处 max 初始化为 -1 
	int min=101;	//题目中所给成绩范围 0 ~ 100,此处 min 初始化为 101
	scanf("%d",&N);	//输入男生、女生的个数
	while(N-->0)	//等同于 for(i=0;i<N;i++)
	{
		scanf("%s %c %s %d",name,&gender,ID,&grade);
		if(gender=='F')	//判断是否为女生,找出成绩最高者,实时刷新数值 
		{
			if(grade>max)
			{
				max=grade;
				strcpy(maxname,name);
				strcpy(maxID,ID);
			}
		}
		if(gender=='M')	//判断是否为男生,找出成绩最低者,实时刷新数值
		{
			if(grade<min)
			{
				min=grade;
				strcpy(minname,name);
				strcpy(minID,ID);
			} 
		}
	}
	if(max==-1)
		printf("Absent\n");
	else
		printf("%s %s\n",maxname,maxID);
	if(min==101)
		printf("Absent\n");
	else
		printf("%s %s\n",minname,minID);
	if(max==-1 || min==101)
		printf("NA\n");
	else
		printf("%d\n",max-min);
	return 0;
}

注:思路和C++代码大体一致,最后输出的判断条件进行了简化。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值