用C#编写学校选拔篮球队员每间宿舍最多有4个人。现给出宿舍列表请找出每个宿舍最高的同学。定义一个学生类Student包含数据成员宿舍号sid姓名 name身高height整数体重weight整数公有构造函数用于初始化数据成员公有方法Display用于输出。输入格式首先输入一个整型数n 1=n=1000000表示n位同学。紧跟着n行输入每一行格式为:宿舍号 name height weight宿舍号
using System; using System.Collections.Generic;
class Student { public string sid;//宿舍号 public string name;//姓名 public int height;//身高 public int weight;//体重
public Student(string sid, string name, int height, int weight)//构造函数初始化数据成员
{
this.sid = sid;
this.name = name;
this.height = height;
this.weight = weight;
}
public void Display()//输出
{
Console.WriteLine(sid + " " + name + " " + height + " " + weight);
}
}
class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine());//读入人数 Dictionary<string, Student> dict = new Dictionary<string, Student>();//用字典存储每个宿舍的最高同学 for (int i = 0; i < n; i++) { string[] s = Console.ReadLine().Split();//读入一行数据 string sid = s[0]; string name = s[1]; int height = int.Parse(s[2]); int weight = int.Parse(s[3]); if (!dict.ContainsKey(sid))//如果该宿舍还没有对应的学生信息 { dict[sid] = new Student(sid, name, height, weight);//直接添加 } else//该宿舍已有对应的学生信息,需要比较身高 { if (height > dict[sid].height)//如果该学生身高更高 { dict[sid] = new Student(sid, name, height, weight);//更新为该学生信息 } } } foreach (var item in dict) { item.Value.Display();//输出每个宿舍的最高同学信息 } }
原文地址: https://www.cveoy.top/t/topic/fcz4 著作权归作者所有。请勿转载和采集!