甲级PAT 1036 Boys vs Girls (25 分)(排序)

本博客介绍了一种算法,用于分析学生群体中男性和女性的最高和最低成绩,并计算两者之间的差值。通过输入学生的性别、ID和分数,该算法能够找到女性最高分和男性最低分的学生,并计算差值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 namegenderID 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

题目要求: 

找出给出学生中,女生的最高分以及男生的最低分,求他们的差值。第一行输出女生的最高分学生的姓名和id,第二行输出男生的最低分学生的姓名和id,最后一行输出女生最高分与男生最低分的差值。如果学生全为男生,则女生对应信息的那一行输出Absent,最后 一行结果输出NA,若全为女生同。

解题思路:

两个数组分别存男生和女生,进行排序。

完整代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct Student{
	string name;
	string ID;
	int grade;
};

Student fstudent[1010];
Student mstudent[1010];
int fnum=0,mnum=0;

bool comp(Student student1,Student student2){
	return student1.grade<student2.grade;
}

int main(){
	int N;
	int i;
	string name,id;
	char gender;
	int grade;
	cin>>N;
	for(i=1;i<=N;i++){
		cin>>name>>gender>>id>>grade;
		if(gender == 'M'){
			mstudent[mnum].name = name; 
			mstudent[mnum].ID = id;
			mstudent[mnum].grade = grade;
			mnum++;
		}else{
			fstudent[fnum].name = name;
			fstudent[fnum].ID = id;
			fstudent[fnum].grade = grade;
			fnum++;
		} 
	}
	sort(mstudent,mstudent+mnum,comp);
	sort(fstudent,fstudent+fnum,comp);
	if(fnum == 0){
		cout<<"Absent"<<endl;
	}else{
		cout<<fstudent[fnum-1].name<<" "<<fstudent[fnum-1].ID<<endl;
	}
	if(mnum == 0){
		cout<<"Absent"<<endl;
	}else{
		cout<<mstudent[0].name<<" "<<mstudent[0].ID<<endl;;
	}
	if(fnum == 0 || mnum == 0){
		cout<<"NA"<<endl;
	}else{
		cout<<fstudent[fnum-1].grade - mstudent[0].grade<<endl;
	}
	return 0;
}

代码简化

2019.1.25

#include<bits/stdc++.h> 
using namespace std;

struct Stu{
	string name;
	string id;
	int grade;
};
Stu mstu[10010],fstu[10010];
int mnum=0,fnum=0;

bool com(Stu s1,Stu s2){
	return s1.grade>=s2.grade;
}

int main(){
	int n,i;
	string name,sex;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>name>>sex;
		if(sex=="M"){
			mstu[mnum].name=name;
			cin>>mstu[mnum].id>>mstu[mnum].grade;
			mnum++;
		}else{
			fstu[fnum].name=name;
			cin>>fstu[fnum].id>>fstu[fnum].grade;
			fnum++;
		}
	}
	sort(mstu,mstu+mnum,com);
	sort(fstu,fstu+fnum,com);
	if(fnum==0) cout<<"Absent"<<endl;
	else cout<<fstu[0].name<<" "<<fstu[0].id<<endl;
	if(mnum==0) cout<<"Absent"<<endl;
	else cout<<mstu[mnum-1].name<<" "<<mstu[mnum-1].id<<endl;
	if(fnum!=0&&mnum!=0) cout<<fstu[0].grade - mstu[mnum-1].grade <<endl;
	else cout<<"NA"<<endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值