PTA 7-3 宿舍谁最高? (10分)

PTA

7-3 宿舍谁最高? (10分)

学校选拔篮球队员,每间宿舍最多有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

答案

#include
#include
using namespace std;
class Student {
public:
    string name;
    int height, weight, room;
    void setdata(int r = 0, string n = 0, int h = 0, int w = 0) {
        room = r, name = n, height = h, weight = w;
    }
    void changedata(Student& s) {
        string c_name;
        int c_height, c_weight, c_room;
        c_name = this->name; c_height = this->height; c_weight = this->weight; c_room = this->room;
        this->name = s.name; this->height = s.height; this->weight = s.weight; this->room = s.room;
        s.name = c_name; s.height = c_height; s.weight = c_weight; s.room = c_room;
    }
};
int  main() {
    int n;
    cin >> n;
    Student s[10000];
    string  name;
    int height, weight, room;
    for (int i = 0; i < n; i++) {
        cin >> room >> name >> height >> weight;
        s[i].setdata(room, name, height, weight);
    }

    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (s[i].room > s[j].room) { s[i].changedata(s[j]); }
        }
    }// ?room??
    for (int i = 0, j = 0, max = 0; i < n;) {
        max = j;
        for (; s[i].room == s[j].room; j++) {
            if (s[j].height >= s[max].height)
            {
                max = j;
            }
        }
        cout << setfill('0') << setw(6)  << s[max].room << " " << s[max].name << " " << s[max].height << " " << s[max].weight << endl;
        i = j;
    }
}

你可能感兴趣的:(C++)