말랑한 하루

[BAEKJOON] 15663 N과 M 9 (Java) 본문

문제풀이/BAEKJOON Online Judge

[BAEKJOON] 15663 N과 M 9 (Java)

지수는말랑이 2021. 1. 23. 15:26
반응형

[ 핵심소스 ]

static LinkedHashSet<String> h = new LinkedHashSet<String>();


[ 소스 코드 ]

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;

public class _15663_N과M_9 {
    static int N, M;
    static int ary[];
    static boolean visit[] = new boolean[9];
	
    static StringBuilder sb = new StringBuilder();
    static LinkedHashSet<String> h = new LinkedHashSet<String>();
	
    static void permutation(int index, int cnt, int temp[]) {
        if (cnt == M) {
            String input="";
            for(int item : temp)
                input+= item+" ";
            h.add(input+"\n");
            return;
        }
        for(int i=0;i<N;i++) {
            if (!visit[i]) {
                visit[i]=true;
                temp[cnt]=ary[i];
                permutation(i, cnt+1, temp);
                visit[i]=false;
            }
        }
    }
	
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        ary=new int[N];
        for(int i=0;i<N;i++)
            ary[i] = sc.nextInt();
        Arrays.sort(ary);
		
        for(int i=0;i<N;i++) {
            int temp[] = new int[M];
            visit[i]=true;
            temp[0]=ary[i];
            permutation(i, 1, temp);
            visit[i]=false;
        }
		
        Iterator<String> iter = h.iterator();
        while(iter.hasNext())
            System.out.print(iter.next());
        sc.close();
    }
}
반응형
Comments