PS/BOJ

백준 6603 로또 / JAVA

얍연구소장 2023. 5. 16.

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; // 출력대상에 미포함
		}
	}

}

'PS > BOJ' 카테고리의 다른 글

백준 1388 바닥장식 / JAVA  (0) 2023.05.18
백준 4963 섬의개수 / JAVA  (0) 2023.05.17
백준 2644 촌수계산 / JAVA  (0) 2023.05.15
백준 1206 보물 / JAVA  (0) 2023.05.10
백준 1996 지뢰찾기 / JAVA  (0) 2023.05.09

댓글