문제링크
fun main() {
val bufferedReader = System.`in`.bufferedReader()
val bufferedWriter = System.out.bufferedWriter()
val testCase = bufferedReader.readLine().toInt()
repeat(testCase) {
val n = bufferedReader.readLine().toInt()
calculateUsingDFS(n, 1, 1, 1, 0, "1")
println()
}
bufferedReader.close()
bufferedWriter.close()
}
fun calculateUsingDFS(n: Int, operand: Int, number: Int, sign: Int, sum: Int, expression: String) {
if (number == n) {
if (sum + sign * operand == 0) println(expression)
return
}
val nextNumber = number + 1
val operandWithSpace = operand * 10 + nextNumber
calculateUsingDFS(n, operandWithSpace, nextNumber, sign, sum, "$expression $nextNumber")
calculateUsingDFS(n, nextNumber, nextNumber, 1, sum + sign * operand, "$expression+$nextNumber")
calculateUsingDFS(n, nextNumber, nextNumber, -1, sum + sign * operand, "$expression-$nextNumber")
}
주석 없는 코드를 만들기 위해 노력하는 개발자입니다.
혹시라도 의도가 분명하지 않아보이는 (이해가 되지 않는) 코드가 있으시다면 편하게 답변 달아주시면 정말 감사하겠습니다.