728x90
문제: https://www.acmicpc.net/problem/15652
코드: https://github.com/irerin07/AlgorithmStudyBaek/blob/master/src/baekjoon/baekjoon15652.java
package baekjoon;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class baekjoon15652 {
static int N;
static int M;
static int[] result;
static StringBuilder sb;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
result = new int[N];
sb = new StringBuilder();
nextPermitation(0);
bw.write(sb.toString());
br.close();
bw.flush();
bw.close();
}
static void nextPermitation(int apply) {
if(apply == M) {
for(int i = 0; i < M; i++) {
sb.append(result[i]+" ");
}
sb.append("\n");
}else {
for(int i = 1; i <= N; i++) {
if (apply != 0 && result[apply-1] > i) {
continue;
}
result[apply] = i;
nextPermitation(apply+1);
}
}
}
}
728x90
'알고리즘' 카테고리의 다른 글
Leet Code 1672 (0) | 2023.07.25 |
---|---|
Leet Code 1480 (0) | 2023.07.25 |
[백준] 피보나치 함수 [Java/ 자바] (0) | 2020.06.14 |
[백준] 피보나치 2 [자바/Java] (0) | 2020.06.07 |
[백준] 크로아티아 알파벳 [Java/자바] (0) | 2020.04.24 |