반응형
package N과M;

import java.util.*;

public class N과M6 {
	public static boolean visit[];
	public static int arr[];
	public static int num[];
	public static int N,M;
	
	
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		
		visit = new boolean[N];
		arr = new int[M];
		num = new int[N];
		for (int i = 0; i < N; i++) {
			num[i] = sc.nextInt();
		}
		Arrays.sort(num);
		permutation(0, 0);
	}
	public static void permutation(int start , int cnt) {
		if(cnt == M) {
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
			return;
		}
		
		for (int i = start; i < N; i++) {
			if(!visit[i]) {
				visit[i] =true;
				arr[cnt] = num[i];
				permutation(i, cnt+1);
				visit[i] =false;
			}
		}
		
		
	}

}

 

반응형

+ Recent posts