백준 -15652번 N 과 M (4)
2025. 4. 8. 18:34
import java.util.Scanner;

public class Main_15652 {

	static int N, M;
	static int[] answer;
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();//전체 데이터 개수(자연수)
		M = scan.nextInt();//선택할 데이터 개수
		
		answer = new int[M];//선택 데이터가 담길 배열
		
		combi(0,1);//인자두개!!  첫번째 인자:선택된 개수(depth)  ,  두번째 인자: 시작위치(start)
		
		scan.close();
	}//main

	private static void combi(int depth, int start) {
		if(depth == M) { //주어진 선택(의 갯수가)이 끝났다면
			for(int num :answer) {
				System.out.print(num+" ");
			}
			System.out.println();
			return;
		}
		
		for(int i= start  ; i<=N; i++) {//for문 ==> 전체 데이터 (1부터 시작하는 자연수를 의미)
			answer[depth]=i;//정답(선택)배열에 값 채우기
			combi(depth+1, i); //중복조합을 만들기 위하여 i+1을 i로 변경 해 준다!!
		}
	}
	

}

'코딩테스트' 카테고리의 다른 글

백준 - 2493번 탑  (0) 2025.04.09
백준 - 2961번 도영이가 만든 맛있는 음식  (0) 2025.04.09
백준 - 15651 N 과 M (3)  (0) 2025.04.08
백준 - 15650번 N 과 M (2)  (0) 2025.04.08
Recursive(재귀)  (0) 2025.04.08