inblog logo
|
LHS's Study Space
    알고리즘문제풀기C#

    [알고리즘 문제 풀기] 생일(5635)

    C#
    lhs's avatar
    lhs
    Jan 25, 2025
    [알고리즘 문제 풀기] 생일(5635)
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리
    www.acmicpc.net
    https://www.acmicpc.net/problem/5635
    notion image

    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

    LHS's Study Space

    RSS·Powered by Inblog