#1 알고리즘
선택 정렬은 알고리즘이 매우 간단하기 때문에, 가벼운 정렬 문제에 사용하기에 안성맞춤이다.
#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
}
}
'문제 풀이 > 기타' 카테고리의 다른 글
[백준] 27522 (카트라이더: 드리프트) (0) | 2023.12.05 |
---|---|
[백준] 2839 (설탕 배달) (0) | 2023.12.04 |
[백준] 4153 (직각삼각형) (0) | 2023.11.30 |
[백준] 8958 (OX퀴즈) (0) | 2023.11.27 |
[백준] 10818 (최소, 최대) (0) | 2023.11.24 |