Written by
java-style
on
on
15650 - N과 M 2(백트래킹)
15650 - N과 M 2(백트래킹)
# 코드 해설
이번엔 저번 문제의 N과 M(1) 보다 훨씬 간단한 문제입니다. 중복된 수열에 대해서는 따로 신경 쓸 필요가 없기 때문에 단순히 for문에 대해서 증가되는 수열 값만 출력해주면 되겠습니다.
import java.util.Scanner; public class Main { public static int[] arr; public static int N, M; public static void main(String[] args) { Scanner in = new Scanner(System.in); N = in.nextInt(); M = in.nextInt(); arr = new int[M]; dfs(1, 0); } public static void dfs(int at, int depth) { if (depth == M) { for (int val : arr) { System.out.print(val + " "); } System.out.println(); return; } for (int i = at; i <= N; i++) { arr[depth] = i; dfs(i + 1, depth + 1); } } }
from http://codingrapper.tistory.com/14 by ccl(A) rewrite - 2021-09-24 00:27:48