15651 N과 M (3) (JAVA)

Fekim·2022년 2월 25일
0

ps

목록 보기
18/48
  • N과 M (1) 문제에서 체크배열만 사용하지 않으면 되는 문제이다.
  • 한가지 주의 할 점은 StringBuilder에 결과를 저장하여 출력을 하면 Pass가 되는 문제인데, System.out. 으로 각각 출력을 하게되면 시간초과가 발생한다는 점이다.
/* 15649 N과 M */
public class Main {

    static int n,m;
    static boolean[] ch;
    static int[] res;
    static StringBuilder sb = new StringBuilder();
    public static void dfs(int L){
        if(L == m){
            for(int i:res)
                if(i != 0)
                    sb.append(i).append(' ');
            sb.append('\n');
            return ;
        }
        else{
            for(int i=1; i<=n; ++i){
               // if(!ch[i]){
                    //ch[i] = true;
                    res[L+1] = i;
                    dfs(L+1);
               // }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        ch = new boolean[n+1];
        res = new int[m+1];
        dfs(0);
        System.out.println(sb);
    }
}
profile
★Bugless 2024★

0개의 댓글