코딩테스트/백준문제

[Java] 백준_15649_N과M(1)

jaewon_sss 2021. 2. 8. 22:11
반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BJ_S3_15649_N과M_1 {
	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static StringBuilder output = new StringBuilder();
	static StringTokenizer tokens;
	static int N,M;
	
	public static void main(String[] args) throws IOException {
		
		tokens = new StringTokenizer(input.readLine());
		N = Integer.parseInt(tokens.nextToken());
		M = Integer.parseInt(tokens.nextToken());
		int [] choosed = new int [M];
		boolean [] visited = new boolean[N];
		permutation(M, choosed, visited);
		////
		System.out.println(output);
	}
	
	static void permutation(int choose, int[] choosed, boolean[] visited) {
		if(choose == 0) {
			for (int val : choosed) {
				output.append(val).append(" ");
			}
			output.append("\n");
			return;
		}
		for (int i = 0; i < N; i++) {
			if(!visited[i]) {
				visited[i]=true;
				choosed[choosed.length - choose] = i+1;
				permutation(choose-1, choosed, visited);
				visited[i] = false;
			}
		}
	}
}
반응형