백준 N과 M(3) (실버 Ⅲ)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class N과M_3 {
static int n;
static int m;
static StringBuilder sb = new StringBuilder();
static int[] path;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
path = new int[m];
recursion(0);
System.out.println(sb);
}
private static void recursion(int level) {
if(level == m) {
for(int value : path) {
sb.append(value + " ");
}
sb.append("\n");
return;
}
for(int i = 1; i <= n; i++) {
path[level] = i ;
recursion(level + 1);
path[level] = 0;
}
}
}
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
속도를 위해서 BufferedReader과 StringTokenizer 사용
'알고리즘' 카테고리의 다른 글
0-1 BFS (1) | 2024.04.04 |
---|---|
[4485] 녹색 옷 입은 애가 젤다지? - Java (0) | 2024.02.05 |
[15650] N과 M(2) - Java (0) | 2023.12.22 |