Baekjoon - 11047

Tadap·2023년 10월 20일
0

Baekjoon

목록 보기
58/94

문제

Solved.ac Class3++

1차시도

public class Main {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int n = Integer.parseInt(split[0]);
		int target = Integer.parseInt(split[1]);
		int[] coins = new int[n];

		for (int i = 0; i < n; i++) {
			coins[i] = Integer.parseInt(br.readLine());
		}

		Arrays.sort(coins);
		int count = 0;
		int selectCoinPosition = n - 1;

		while (target != 0) {
			int selectCoin = coins[selectCoinPosition];
			if (selectCoin <= target) {
				while (target >= selectCoin) {
					count++;
					target -= selectCoin;
				}
			}
			selectCoinPosition--;
		}
		System.out.println(count);
	}
}

성공

ToKotlin

fun main() {
    val split = readln().split(" ")
    val n = split[0].toInt()
    var target = split[1].toInt()
    val coins = Array(n) { 0}

    for (i in 0..<n) {
        coins[i] = readln().toInt()
    }

    coins.sort()

    var count: Int = 0

    var selectCoinPosition = n - 1

    while (target != 0) {
        val selectCoin = coins[selectCoinPosition]
        if (selectCoin <= target) {
            while (target >= selectCoin) {
                target -= selectCoin
                count++
            }
        }
        selectCoinPosition--
    }
    print(count)
}

0개의 댓글