[25305] 커트라인

RudinP·2023년 4월 6일
0

BaekJoon

목록 보기
20/77

2022 연세대학교 미래캠퍼스 슬기로운 코딩생활에
NN명의 학생들이 응시했다.
이들 중 점수가 가장 높은
kk명은 상을 받을 것이다. 이 때, 상을 받는 커트라인이 몇 점인지 구하라.
커트라인이란 상을 받는 사람들 중 점수가 가장 가장 낮은 사람의 점수를 말한다.

입력

  • 첫째 줄에는 응시자의 수
    NN과 상을 받는 사람의 수
    kk가 공백을 사이에 두고 주어진다.

둘째 줄에는 각 학생의 점수
xx가 공백을 사이에 두고 주어진다.

출력

  • 상을 받는 커트라인을 출력하라.

제한

1N10001 ≤ N ≤ 1\,000

1kN1 ≤ k ≤ N

0x100000 ≤ x ≤ 10\,000

생각

1) 정렬한다. : 셸 정렬 사용 예정
2) 인덱스가 k-1 인 수를 출력한다.

처음 코드

namespace SongE
{
    public class Program
    {
        static void InsertionSort(int[] list, int first, int last, int gap)
        {
            int i, j, key;

            for (i = first + gap; i <= last; i += gap)
            {
                key = list[i];

                for (j = i - gap; j >= first && list[j] > key; j -= gap)
                {
                    list[j + gap] = list[j];
                }

                list[j + gap] = key;
            }
        }

        static void ShellSort(int[] list, int n)
        {
            int i, gap;
            for (gap = n / 2; gap > 0; gap /= 2)
            {
                if ((gap % 2) == 0)
                {
                    gap++;
                }

                for (i = 0; i < gap; i++)
                {
                    InsertionSort(list, i, n - 1, gap);
                }
            }
        }
        static void Main(string[] args)
        {
            int[] nk = Array.ConvertAll(Console.ReadLine().Split(), s => int.Parse(s));

            int[] list = Array.ConvertAll(Console.ReadLine().Split(), s => int.Parse(s));
            int cut = nk[1];

            ShellSort(list, nk[0]);

            Console.WriteLine(list[nk[1] - 1]);
        }
    }
}

틀렸다.
사유: 오름차순으로 해두고 앞에서 두번째 수를 출력하게 함

두번째 시도

namespace SongE
{
    public class Program
    {
        static void InsertionSort(int[] list, int first, int last, int gap)
        {
            int i, j, key;

            for (i = first + gap; i <= last; i += gap)
            {
                key = list[i];

                for (j = i - gap; j >= first && list[j] < key; j -= gap)
                {
                    list[j + gap] = list[j];
                }

                list[j + gap] = key;
            }
        }

        static void ShellSort(int[] list, int n)
        {
            int i, gap;
            for (gap = n / 2; gap > 0; gap /= 2)
            {
                if ((gap % 2) == 0)
                {
                    gap++;
                }

                for (i = 0; i < gap; i++)
                {
                    InsertionSort(list, i, n - 1, gap);
                }
            }
        }
        static void Main(string[] args)
        {
            int[] nk = Array.ConvertAll(Console.ReadLine().Split(), s => int.Parse(s));

            int[] list = Array.ConvertAll(Console.ReadLine().Split(), s => int.Parse(s));
            int cut = nk[1];

            ShellSort(list, nk[0]);

            Console.WriteLine(list[cut - 1]);
        }
    }
}

오름차순으로 바꿔주었다.

profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글