Baekjoon algorithm training

[백준] 9076 (점수 집계)

interfacer_han 2023. 12. 2. 11:10

#1 알고리즘

 

선택 정렬 (Selection Sort)

#1 알고리즘 #2 요약 (자바) public static void selectionSort(int[] array, int startIndex, int endIndex) { for(int maxIndex = endIndex; startIndex < maxIndex; maxIndex--) { // 배열을 순회하며 가장 값이 큰 원소의 index를 구한다 int

kenel.tistory.com

선택 정렬은 알고리즘이 매우 간단하기 때문에, 가벼운 정렬 문제에 사용하기에 안성맞춤이다.

 

#2 코드

#2-1 자바

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int testCaseCount = Integer.parseInt(sc.nextLine().trim());
        for(int i = 0; i < testCaseCount; i++) {
            String[] scoresStr = sc.nextLine().trim().split(" ");
            int[] scores = new int[5];
            for(int j = 0; j <= 4; j++) {
                scores[j] = Integer.parseInt(scoresStr[j]);
            }
            
            judgeScore(scores); 
        }
        
        sc.close();
    }
    
    private static void judgeScore(int[] scores) {
        // 오름차순 정렬
        selectionSort(scores, 0, scores.length - 1);
        
        if(scores[3] - scores[1] >= 4) {
            System.out.println("KIN");
            
        } else {
            int scoreSum = 0;
            for(int i = 1; i <= 3; i++) {
                scoreSum += scores[i];
            }
            System.out.println(Integer.toString(scoreSum));
        }
    }
    
    private static void selectionSort(int[] array, int startIndex, int endIndex) {
        for(int maxIndex = endIndex; startIndex < maxIndex; maxIndex--) {
            int indexOfLargest = startIndex;
            for(int i = (startIndex + 1); i <= maxIndex; i++) {
                if(array[indexOfLargest] < array[i]) {
                    indexOfLargest = i;
                }
            }
        
            int temp = array[maxIndex];
            array[maxIndex] = array[indexOfLargest];
            array[indexOfLargest] = temp;
        }
    }
}

 

#2-2 코틀린

fun main() {
    val testCaseCount = readln()!!.trim().toInt()
    
    for(i : Int in 0..<testCaseCount) {
        val scoresStr = readln()!!.trim().split(" ")
        val scores : Array<Int> = Array(5) { 0 }
        for(j : Int in 0..4) {
            scores[j] = scoresStr[j].toInt()
        }
        
        judgeScore(scores)
    }
}

fun judgeScore(scores : Array<Int>) {
    // 오름차순 정렬
    selectionSort(scores, 0, scores.size - 1)
    
    if(scores[3] - scores[1] >= 4) {
        println("KIN")
    } else {
        var scoreSum = 0
        for(i : Int in 1..3) {
            scoreSum += scores[i]
        }
        println("${scoreSum}")
    }
}

fun selectionSort(array: Array<Int>, startIndex : Int, endIndex : Int) {
    for(maxIndex : Int in endIndex downTo (startIndex + 1)) {
        var indexOfLargest = startIndex;
        for(i : Int in (startIndex + 1)..maxIndex) {
            if(array[indexOfLargest] < array[i]) {
                indexOfLargest = i
            }
        }

        val temp = array[maxIndex]
        array[maxIndex] = array[indexOfLargest]
        array[indexOfLargest] = temp
    }
}