【C++ | PTA】宿舍谁最高?(map容器)

文章目录

题目要求

学校选拔篮球队员,每间宿舍最多有 4 个人。现给出宿舍列表,请找出每个宿舍最高的同学。定义一个学生类 Student,有身高 height,体重 weight 等。
输入格式:

首先输入一个整型数 n (1≤n≤106),表示有 n 位同学。
紧跟着 n 行输入,每一行格式为:宿舍号 name height weight。
宿舍号的区间为 [0, 999999], name 由字母组成,长度小于 16,height,weight 为正整数。

输出格式:

按宿舍号从小到大排序,输出每间宿舍身高最高的同学信息。题目保证每间宿舍只有一位身高最高的同学。
注意宿舍号不足 6 位的,要按 6 位补齐前导 0。

输入样例:

7
000000 Tom 175 120
000001 Jack 180 130
000001 Hale 160 140
000000 Marry 160 120
000000 Jerry 165 110
000003 ETAF 183 145
000001 Mickey 170 115

输出样例:

000000 Tom 175 120
000001 Jack 180 130
000003 ETAF 183 145

代码

#include<string>
#include<iostream>
#include<iomanip>
#include<map>
using namespace std;
class Student
{
public:
	int dormitory;
	string name;
	int height;
	int weight;
};


int main(void)
{
	int n;
	cin >> n;
	if (n < 1 || n>1000000)
		exit(1);  //如果输入的 n 不在指定范围内(小于1或大于1000000),程序会执行 exit(1),即退出程序并返回值 1

	Student* stus = new Student[n];//创建n个Student对象

	//用map来存各宿舍最高的人的信息:宿舍号、姓名、身高;int存宿舍号,Student类存姓名和最高的身高
	map<int, Student>stu_map;                         

	
	for (int i = 0; i < n; i++)
	{
		cin >> stus[i].dormitory >> stus[i].name >> stus[i].height >> stus[i].weight;  //录入全部学生信息
		if (stu_map[stus[i].dormitory].height < stus[i].height)//在同一个宿舍中进行身高排序
		{
			stu_map[stus[i].dormitory].name = stus[i].name;
			stu_map[stus[i].dormitory].height = stus[i].height;
			stu_map[stus[i].dormitory].weight = stus[i].weight;
		}
	}

//数据输出
	for (map<int,Student>::iterator iter = stu_map.begin(); iter != stu_map.end(); iter++)
	{
		cout << setfill('0') << setw(6) << iter->first << " ";
		cout<<iter->second.name << " " << iter->second.height << " " << iter->second.weight << endl;
	}

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋说

感谢打赏,祝你平安喜乐。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值