运用C#语言学校选拔篮球队员每间宿舍最多有4个人。现给出宿舍列表请找出每个宿舍最高的同学。定义一个学生类Student包含数据成员宿舍号sid姓名 name身高height整数体重weight整数公有构造函数用于初始化数据成员公有方法Display用于输出。输入格式首先输入一个整型数n 1=n=1000000表示n位同学。紧跟着n行输入每一行格式为:宿舍号 name height weight宿舍
using System; using System.Collections.Generic;
namespace P1135 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Student[] students = new Student[n];
for (int i = 0; i < n; i++)
{
string[] input = Console.ReadLine().Split(' ');
students[i] = new Student(input[0], input[1], int.Parse(input[2]), int.Parse(input[3]));
}
Dictionary<string, Student> maxStudents = new Dictionary<string, Student>();
foreach (Student student in students)
{
if (!maxStudents.ContainsKey(student.Sid))
{
maxStudents[student.Sid] = student;
}
else if (maxStudents[student.Sid].Height < student.Height)
{
maxStudents[student.Sid] = student;
}
}
List<Student> result = new List<Student>();
foreach (Student maxStudent in maxStudents.Values)
{
result.Add(maxStudent);
}
result.Sort((a, b) => string.Compare(a.Sid, b.Sid));
foreach (Student student in result)
{
student.Display();
}
}
}
class Student
{
public string Sid { get; }
public string Name { get; }
public int Height { get; }
public int Weight { get; }
public Student(string sid, string name, int height, int weight)
{
Sid = sid;
Name = name;
Height = height;
Weight = weight;
}
public void Display()
{
Console.WriteLine($"{Sid} {Name} {Height} {Weight}");
}
}
原文地址: https://www.cveoy.top/t/topic/fcld 著作权归作者所有。请勿转载和采集!