15657 - N과 M 8(백트래킹)

15657 - N과 M 8(백트래킹)

# 주소

https://www.acmicpc.net/problem/15657

# 문제

# 문제 해설 및 코드 리뷰

package first; import java.util.*; import java.util.Scanner; public class sex { public static int[] arr; public static int N, M; public static int[] ans; public static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { Scanner in = new Scanner(System.in); N = in.nextInt(); M = in.nextInt(); arr = new int[M]; ans = new int[N]; for(int i = 0; i < N; i++) { ans[i] = in.nextInt(); } Arrays.sort(ans); dfs(0,0); System.out.print(sb); } public static void dfs(int t,int depth) { if (depth == M) { for (int val : arr) { sb.append(val + " "); } sb.append('

'); return; } for (int i = t ; i < N; i++) { arr[depth] = ans[i]; dfs(i,depth + 1); } } }

이번에는 중복을 허용하면서 비내림차순으로 출력하게끔 문제가 유도되었습니다.

단순하게 시작점을 i = t로 설정하고 for문이 반복될 때 마다 증가된 i값을 가지게 코드를 짜시면 되겠습니다.

from http://codingrapper.tistory.com/20 by ccl(A) rewrite - 2021-09-24 21:27:18