
1. 문제 풀이 아이디어
- 이름과 생일을 가진 클래스를 비교 가능하도록 구현하여 문제를 해결할 수 있다.
2. 나의 정답 코드
StreamReader sr = new(Console.OpenStandardInput());
StreamWriter sw = new(Console.OpenStandardOutput());
int n = int.Parse(sr.ReadLine());
List<Person> persons = new List<Person>();
for (int i = 0; i < n; i++)
{
string[] split = sr.ReadLine().Split();
persons.Add(new Person(split[0], int.Parse(split[1]), int.Parse(split[2]), int.Parse(split[3])));
}
persons.Sort();
sw.WriteLine(persons[0].Name);
sw.WriteLine(persons[persons.Count - 1].Name);
sr.Close();
sw.Close();
class Person : IComparable<Person>
{
public string Name { get; set; }
public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public Person(string name, int day, int month, int year)
{
Name = name;
Day = day;
Month = month;
Year = year;
}
public int CompareTo(Person? other)
{
if (other == null)
return 1;
if (other.Year == this.Year)
{
if (other.Month == this.Month)
{
return other.Day - this.Day;
}
return other.Month - this.Month;
}
return other.Year - this.Year;
}
}3. 정리
Person클래스를 정의하여 이름과 생일을 속성으로 갖고,IComparable<Person>인터페이스를 구현하여 비교 가능하도록 한다.
CompareTo메서드에서Year,Month,Day를 기준으로 내림차순 비교를 수행한다.
persons.Sort()를 사용해 리스트를 정렬한 후, 첫 번째와 마지막 사람의 이름을 출력한다.
Share article