7-1 宿舍谁最高?

该博客介绍了一道算法问题,即如何从给定的宿舍名单中找出每个宿舍身高最高的学生。输入包括宿舍号、姓名、身高和体重,输出要求按照宿舍号排序,列出每个宿舍的最高身高学生信息。博客提供了输入输出示例,并提示解决方案并不复杂。
摘要由CSDN通过智能技术生成

学校选拔篮球队员,每间宿舍最多有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<iostream>
#include <iomanip>
#include <string>
using namespace std;

class Student{
   
public: 
    void set(int ord,string s,int x,int y){
   
        order=ord;
        name=s;
        height=x;
        weight=y;
    }
     int 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
思路:首先需要定义一个学生类 Student,包含身高和体重两个属性。然后需要定义一个宿舍类 Dormitory,包含宿舍号、宿舍人数和宿舍成员列表等属性,以及获取宿舍最高同学的方法 getMaxStudent()。 接下来,遍历宿舍列表,对每个宿舍调用 getMaxStudent() 方法,得到最高同学的信息,并输出。 代码实现如下: ``` class Student: def __init__(self, height, weight): self.height = height self.weight = weight class Dormitory: def __init__(self, number, count): self.number = number self.count = count self.members = [] def addMember(self, student): self.members.append(student) def getMaxStudent(self): max_height = 0 max_student = None for student in self.members: if student.height > max_height: max_height = student.height max_student = student return max_student # 测试数据 dorm_list = [] dorm1 = Dormitory(1, 3) dorm1.addMember(Student(170, 60)) dorm1.addMember(Student(175, 62)) dorm1.addMember(Student(180, 65)) dorm_list.append(dorm1) dorm2 = Dormitory(2, 2) dorm2.addMember(Student(165, 55)) dorm2.addMember(Student(170, 58)) dorm_list.append(dorm2) dorm3 = Dormitory(3, 4) dorm3.addMember(Student(175, 65)) dorm3.addMember(Student(180, 68)) dorm3.addMember(Student(170, 62)) dorm3.addMember(Student(185, 70)) dorm_list.append(dorm3) # 输出每个宿舍最高的同学 for dorm in dorm_list: print("宿舍{}最高的同学身高为{}".format(dorm.number, dorm.getMaxStudent().height)) ``` 输出结果为: ``` 宿舍1最高的同学身高为180 宿舍2最高的同学身高为170 宿舍3最高的同学身高为185 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值