알고리즘/백트래킹
[백준 풀이_Java] 73447483 로또 (실버2)
happyhelen
2024. 2. 15. 10:18
백트래킹을 사용해서 풀었다.
재귀함수 안에서 어떤 값을 올리느냐에 따라 결과가 달라진다는 것을 알았다.
여기서는 i=start 인데 i를 올릴지 start를 올릴지 잘 생각해야한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int[] arrNum;
static int K;
static int[] answer;
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String[] arrK = br.readLine().split(" ");
K = Integer.parseInt(arrK[0]);
if(K == 0) break;
arrNum = new int[K];
for(int i=1; i<arrK.length; i++){
arrNum[i-1] = Integer.parseInt(arrK[i]);
}
answer = new int[6];
visited = new boolean[K];
recur(0, 0);
System.out.println();
}
}
static void recur(int start, int cnt){
// if 6
if(cnt == 6){
for (int i=0; i<6; i++){
System.out.print(answer[i]+" ");
}
System.out.println();
return;
}
for(int i=start; i<K; i++){
answer[cnt] = arrNum[i];
recur(i+1,cnt+1); // 무슨 값을 올리느냐 확인
}
}
}