본문 바로가기
알고리즘 & 자료구조/백준

백준 15652

by 신재권 2021. 8. 25.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main15652 {
	
	public static int N, M;
	public static int[] selected; 
	public static StringBuilder sb = new StringBuilder();
	

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		N = Integer.parseInt(st.nextToken()); // 1~N 까지4
		M = Integer.parseInt(st.nextToken()); // M개를 고른 수열 
		selected = new int[M+1]; //결과 수열
		rec_func(1);
		System.out.println(sb.toString());
		
		
	}
	
	public static void rec_func(int k){
		if(k == M + 1){ //다 골랐을때
			//selected[1...M] 배열이 새롭게 탐색된 결과
			for(int i=1;i <=M; i++) {
				sb.append(selected[i]).append(' ');
			}
			sb.append('\n');
		}else{
			// 1~N 까지를 k번 원소로 한번씩 정하고
			// 매번 k+1 번부터 M번 원소로 재귀호출 해주기
			int start = selected[k-1];
			if(start == 0) start = 1;
			for(int cand = start; cand <= N; cand++){
				selected[k] = cand;
				//k+1 번 ~ M번을 모두 탐색하는 일을 해야하는 상황
				rec_func(k+1);
				selected[k] = 0; 
			}
		}
	}
	
	

}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 11722  (0) 2021.08.30
백준 11053  (0) 2021.08.26
백준 2193  (0) 2021.08.25
백준 15649  (0) 2021.08.24
백준 15651  (0) 2021.08.24