学校选拔篮球队员,每间宿舍最多有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
很裸的一道map题目。注意点:C++结构体类似于类,可以
student(){
height = 0;
weight = 0; 初始化值。mp是一个类字典,map中的元素是关键字-值对,可以用mp[关键字]的方法得到值,如果该关键字不
}
存在则会新建一个,并且mp遍历要用迭代器。mp中存的是每个宿舍最高的,并且map会根据first排序,输入数据比较大正好省去了排序的时间。
#include<iostream>
#include<cstdio>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 1000000+2;
struct student{
student(){
height = 0;
weight = 0;
}
string name;
int height;
int weight;
};
student students[maxn];
map<int,student>mp;
int main()
{
int n;
cin>>n;
int num;
for(int i = 0;i<n;i++){
cin>>num;
cin>>students[i].name>>students[i].height>>students[i].weight;
if(mp[num].height < students[i].height){ //mp记录每个班宿舍最高的同学。mp[num]键值(num)不存在会新建
mp[num] = students[i];
}
}
map<int,student>::iterator i;
for(i = mp.begin();i != mp.end();i++){
printf("%06d",i->first);
cout<<" "<<i->second.name<<" "<<i->second.height<<" "<<i->second.weight<<endl;
}
}