하루 한 접시
[백준] 25305번: 커트라인 [C#]
NaZZU
2024. 3. 23. 00:07
첫째 줄에는 참가 인원 수, 커트라인이 문자열 형식으로 한 줄로 주어지고
둘째 줄에는 각 참가자들의 점수가 주어진다.
참가자 중 2등까지 상을 수여한다고 한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
namespace 연습장
{
internal class Program
{
static void Main(string[] args)
{
using var reader = new StreamReader(Console.OpenStandardInput());
using var print = new StreamWriter(Console.OpenStandardOutput());
//StringBuilder sb = new StringBuilder();
string[] input = reader.ReadLine().Split();
int N = int.Parse(input[0]);
int cut = int.Parse(input[1]);
string[] getScore = reader.ReadLine().Split();
int[] score = new int[N];
for (int i = 0; i < N; i++)
score[i] = int.Parse(getScore[i]);
Array.Sort(score);
Array.Reverse(score);
print.WriteLine(score[--cut]);
}
}
}
|
cs |
순위를 내림차순 정렬을 한 후, 주어진 커트라인을 이용해 인덱싱을 한다.
참고로 배열은 0번부터 시작하므로 커트라인을 1 감소시켜줘야 한다.
내가 사용한 방법은 데이터의 값을 변형시키므로 코드를 다시 사용해야 할 경우 문제가 될 수 도 있다.
또 다른 방법으로는 그냥 score.Length - cut 을 하면 내림차순 정렬을 할 필요가 없다.