PS/BOJ
백준 6603 로또 / JAVA
얍연구소장
2023. 5. 16. 22:14
https://www.acmicpc.net/problem/6603
6603번: 로또
입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로
www.acmicpc.net
>풀이
/*
* 백준 6603 로또
* #실버2
* #DFS
*/
public class boj_6603 {
static int k; // 49개 중 k
static int[] S; // 길이가 k 인 집합 S
static boolean[] checked;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true) {
String input = br.readLine();
if(input.equals("0")) break;
String[] arr = input.split(" ");
k = Integer.parseInt(arr[0]);
S = new int[k];
checked = new boolean[k];
for(int i = 0; i < k; i++) S[i] = Integer.parseInt(arr[i+1]);
DFS(0,0);
System.out.println();
}
}
public static void DFS(int start, int depth) {
if(depth == 6) {
//로또의 갯수인 6개가 모였을 때 체크
for(int i = 0; i < k; i++) {
if(checked[i]) System.out.print(S[i] + " ");
}
System.out.println();
}
for(int i = start; i < k; i++) {
checked[i] = true; // 출력대상에 포함
DFS(i + 1, depth + 1);
checked[i] = false; // 출력대상에 미포함
}
}
}