📕 문제
📌 링크
![](https://velog.velcdn.com/images/wowns226/post/814858bc-76ec-4d37-83cf-6c28a3e0e3e2/image.png)
📗 접근 방식
- 나라를 나타내는 Country 클래스를 정의하고 개인 번호, 금메달, 은메달, 동메달, 순위 데이터를 저장하고 있다.
- 나라들의 정보를 입력받고 Country 객체로 생성하여 리스트에 저장한다.
- 나라들을 금메달과 은메달과 동메달에 대한 내림차순 정렬한다.
- 정렬된 나라 리스트를 순회하면서 순위를 부여합니다. 동점인 경우에는 같은 순위를 부여합니다.
- 주어진 나라의 개인 번호를 검색하고 해당 나라의 순위를 출력합니다.
📘 코드
namespace BOJ
{
class No_8979
{
class Country
{
public int PersonalNumber { get; } = 0;
public int Gold { get; } = 0;
public int Silver { get; } = 0;
public int Bronze { get; } = 0;
public int Rank { get; set; } = 0;
public Country(int personalNumber, int gold, int silver, int bronze)
{
PersonalNumber = personalNumber;
Gold = gold;
Silver = silver;
Bronze = bronze;
}
public bool IsSameOtherCountry(Country other)
{
if(Gold == other.Gold && Silver == other.Silver && Bronze == other.Bronze)
return true;
return false;
}
}
static int n, k;
static List<Country> countryList = new List<Country>();
public static void Main()
{
int[] inputs = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
n = inputs[0];
k = inputs[1];
for(int i=0 ;i<n ;i++)
{
inputs = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
Country newCountory = new Country(inputs[0], inputs[1], inputs[2], inputs[3]);
countryList.Add(newCountory);
}
var sortedList = countryList.OrderByDescending(x => x.Gold).ThenByDescending(x => x.Silver).ThenByDescending(x => x.Bronze).ToList();
int rank = 1;
sortedList[0].Rank = rank;
Country before = sortedList[0];
for(int i=1 ;i<sortedList.Count ;i++)
{
rank++;
if(sortedList[i].IsSameOtherCountry(before))
{
sortedList[i].Rank = before.Rank;
}
else
{
sortedList[i].Rank = rank;
}
before = sortedList[i];
}
for(int i=0 ;i<sortedList.Count ;i++)
{
if(sortedList[i].PersonalNumber == k)
{
Console.WriteLine(sortedList[i].Rank);
return;
}
}
}
}
}
📙 오답노트
📒 알고리즘 분류