学校选拔篮球队员,每间宿舍最多有 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
以下是C++的代码实现:
#include <iostream>
#include<iomanip>
using namespace std;
class student
{
public:
int id;
string name;
int height;
int weight;
student(){id = -1;name = " "; height = 0; weight = 0;}
};
int main()
{
int n, maxroom = 0;
cin >> n;
student *pstu = new student[n];
for(int i = 0; i < n; i++)
{
cin >> pstu[i].id >> pstu[i].name >> pstu[i].height >> pstu[i].weight;
if(maxroom <= pstu[i].id + 1)
{
maxroom = pstu[i].id + 1;
}
}
int *p = new int [maxroom]; //每个宿舍对应的最大值
for(int i = 0; i < maxroom; i++)
{
p[i] = -1;
}
for(int i = 0; i < n; i++)
{
if(pstu[i].height >= pstu[p[pstu[i].id]].height)
{
p[pstu[i].id] = i;
}
}
for(int i = 0; i < maxroom; i++)
{
if(p[i] != -1)
{
cout << setfill('0') << setw(6) << i << " " << pstu[p[i]].name << " " << pstu[p[i]].height << " " << pstu[p[i]].weight << endl;
}
}
delete[] p;
delete[] pstu;
}
前置0的输出格式参考了如下文章:
[C++]如何输出数字时在前面加0_MagicBiao的博客https://blog.csdn.net/MagicBiao/article/details/80513307