[JAVA/2693번] N번째 큰 수*

고지훈·2021년 12월 16일
1

Algorithm

목록 보기
57/68
post-thumbnail

문제


입력 및 출력


풀이


import java.io.*;
import java.util.*;

class Main {
    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // T
        int T = Integer.parseInt(br.readLine());

        // 결과 값 추출할 변수
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < T; i++) {
            String[] temp = br.readLine().split(" ");

            // 숫자 배열로 변환과정
            int[] array = new int[temp.length];
            for (int j = 0; j < temp.length; j++) {
                array[j] = Integer.parseInt(temp[j]);
            }

            // 3번째 큰 수를 추출
            sb.append(SelectionSort(array) + "\n");
        }

        // 결과 값 출력
        System.out.println(sb);
    }

    public static int SelectionSort(int[] array) {
        int minIndex = 0;
        for (int i = 0; i < array.length; i++) {
            minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }

            // swap 교환
            int temp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = temp;
        }

        // 결과 값 반환
        return array[array.length - 3];
    }
}

결과 및 해결방법

[결과]

profile
"계획에 따르기보다 변화에 대응하기를"

0개의 댓글