1036 Boys vs Girls (25 point(s))

本文介绍了一个简单的算法,用于分析学生数据集中的性别成绩差异。通过读取输入文件,该算法将学生分为男性和女性两组,并找出每组中成绩最高和最低的学生。最后,它计算并输出女性最高成绩与男性最低成绩之间的差距。

1036 Boys vs Girls (25 point(s))

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

简单排序题。 

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int flag;
struct student{
	string name,id;int grade;
	student(string n,string i,int g):name(n),id(i),grade(g){}
	bool operator < (const student &other) const{
		if(flag==1) return grade<other.grade;
		else if(flag==2) return  grade>other.grade;
	}
};
vector<student> boys;
vector<student> girls;
int main(void){
	int n;cin>>n;
	string name,gender,id;int grade;
	while(n--){
		cin>>name>>gender>>id>>grade;
		if(gender=="M") boys.push_back(student(name,id,grade));
		if(gender=="F") girls.push_back(student(name,id,grade));
	}
	flag = 1;sort(boys.begin(),boys.end());
	flag = 2;sort(girls.begin(),girls.end());
	
	if(girls.size()==0) cout<<"Absent"<<endl;
	else  cout<< girls[0].name<<" "<<girls[0].id<<endl;
	
	if(boys.size()==0) cout<<"Absent"<<endl;
	else cout<< boys[0].name<<" "<<boys[0].id<<endl; 
	 
	if(boys.size()==0||girls.size()==0) cout<<"NA"<<endl;
	else cout<<girls[0].grade-boys[0].grade<<endl;
	return 0;
}

 

### 关于1036 Boys vs Girls 测试点2 的解决方案 针对给定的代码片段[^5],该程序旨在通过输入的一系列字符串来判断某些条件是否满足。具体而言,在每组数据中读取n对学生的名字(s1, s2),其中s1代表男生名字而s2代表女生名字,并分别存入集合`A`和`B`中。之后比较两个集合大小之差是否等于1以决定输出"Yes"或"No"。 然而,对于测试点2的具体问题未直接提及,通常这类题目中的挑战可能涉及边界情况处理不当或是逻辑错误等问题。为了更好地理解和解决问题,考虑以下几个方面: - **输入验证**:确保所有预期之外的情况都能被妥善处理,比如当输入为空或其他异常情形时。 - **效率优化**:虽然当前实现已经使用了高效的数据结构——set来进行去重操作,但在极端情况下仍需关注性能表现。 考虑到上述因素,下面提供了一种改进后的C++版本代码示例,用于更稳健地应对各种输入场景并提高可读性和维护性: ```cpp #include <iostream> #include <unordered_set> using namespace std; int main() { unordered_set<string> boys; unordered_set<string> girls; int cases; cin >> cases; while (cases--) { int pairsCount; cin >> pairsCount; bool valid = true; for (int i = 0; i < pairsCount && valid; ++i) { string boyName, girlName; if (!(cin >> boyName >> girlName)) { // 输入失败则认为无效案例 valid = false; break; } boys.insert(boyName); girls.insert(girlName); // 如果任何时候发现有重复的名字,则立即判定为不合法 if ((boys.find(girlName) != boys.end()) || (girls.find(boyName) != girls.end())) { valid = false; } } cout << (valid && abs(static_cast<int>(boys.size()) - static_cast<int>(girls.size())) == 1 ? "Yes\n" : "No\n"); // 清空容器准备下一个case boys.clear(); girls.clear(); } return 0; } ``` 此代码增加了对非法输入的支持以及提前终止循环的能力,从而提高了健壮性。此外,还引入了`abs()`函数用来计算绝对值差异,使得即使在性别比例颠倒的情况下也能给出正确答案。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值