알고리즘

<백준> 15652번 자바 알고리즘

changha. 2021. 12. 31. 23:13
import java.util.*;

public class Main
{   
    static int N, M;
    static int[] arr;
    static StringBuilder sb = new StringBuilder();
    
	public static void main(String[] args) {
	    
	    arr = new int[8];
	    
	    Scanner input = new Scanner(System.in);
	    N = input.nextInt();
	    M = input.nextInt();
	    
	    recursion(0);
	    System.out.println(sb);
	}
	
	private static void recursion(int idx){
	    if(idx == M){
	        for(int i = 0; i < M; i++){
	            sb.append(arr[i] + " ");
	        }
	        sb.append('\n');
	        return;
	    }
	    
	    for(int i = 1; i <= N; i++){
	       if(idx != 0 && arr[idx - 1] > i) continue;
	       
	       arr[idx] = i;
	       recursion(idx + 1);
	       
	       
	       
	    }
	    
	}
}