7-4 宿舍谁最高? (20 分)

7-4 宿舍谁最高? (20 分)

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

输入格式:

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

输出格式:

按宿舍号从小到大排序,输出每间宿舍身高最高的同学信息。题目保证每间宿舍只有一位身高最高的同学。

输入样例:

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

直接把样例用结构体加数组存起来加sort排序会超内存,所以用map,不存数据,直接比较。map会自动对key进行排序,所以输出的宿舍号也是经过排序过的。

#include<iostream>
#include<map>
#include<cstring>
using namespace std;

struct node{
	string name;
	int height;
	int weight;
};

int main()
{
	map<int,node>a;
	int i,n;
	cin>>n;
	for(i=0;i<n;i++)
	{
		int num;
		string str;
		int x,y;
		cin>>num>>str>>x>>y;
		if(a[num].height<x)
		{
			a[num].name=str;
			a[num].height=x;
			a[num].weight=y;
		}
	}
	map<int,node>::iterator it=a.begin();
	for(;it!=a.end();it++)
	{
		printf("%06d",it->first);
		cout<<" "<<it->second.name<<" "<<it->second.height<<" "<<it->second.weight<<endl;
	}
	return 0;
}
发布了73 篇原创文章 · 获赞 15 · 访问量 8126

猜你喜欢

转载自blog.csdn.net/ln2037/article/details/88658385