Baekjoon - 15650

Tadap·2023년 11월 4일
0

Baekjoon

목록 보기
72/94
post-custom-banner

문제

Solved.ac Class4

1차시도

public class Main {
	private static boolean[] isVisit;
	private static int size;
	private static StringBuilder sb;
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();

		String[] split = br.readLine().split(" ");
		size = Integer.parseInt(split[0]);
		int target = Integer.parseInt(split[1]);
		isVisit = new boolean[size + 1];

		for (int i = 1; i < size + 2 - target; i++) {
			isVisit[i] = true;
			solve(i, 1, target);
			isVisit[i] = false;
		}
		System.out.println(sb);
	}

	private static void solve(int start, int deep, int target) {
		if (deep == target) {
			print();
			return;
		}

		for (int i = start + 1; i < size + 1; i++) {
			isVisit[i] = true;
			solve(i, deep + 1, target);
			isVisit[i] = false;
		}
	}

	private static void print() {
		for (int i = 1; i < size + 1; i++) {
			if (isVisit[i]) {
				sb.append(i).append(" ");
			}
		}
		sb.append("\n");
	}
}

성공

ToKotlin

val sb = StringBuilder()
var isVisit: BooleanArray = BooleanArray(0)
var size: Int = 0

fun main() {

    val split = readln().split(" ")
    size = split[0].toInt()
    val target = split[1].toInt()
    isVisit = BooleanArray(size + 1)

    for (i in 1..<size + 2 - target) {
        isVisit[i] = true
        solve(i, 1, target)
        isVisit[i] = false
    }
    print(sb)
}

fun solve(start: Int, deep: Int, target: Int) {
    if (deep == target) {
        print()
        return
    }
    for (i in start + 1..size) {
        isVisit[i] = true
        solve(i, deep + 1, target)
        isVisit[i] = false
    }

}

fun print() {
    for (i in 1..size) {
        if (isVisit[i]) {
            sb.append(i).append(" ")
        }
    }
    sb.append("\n")
}

post-custom-banner

0개의 댓글