백준/재귀
6603 로또 (java)
have a good time
2022. 2. 1. 22:19
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int number[];
static boolean result[];
static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
if(N == 0) {
break;
}
number = new int [N];
result = new boolean[N];
for(int i =0; i<N; i++) {
number[i] = Integer.parseInt(st.nextToken());
}
dfs(0,0);
System.out.println();
}
}
public static void dfs (int start, int depth) {
if(depth == 6) {
for(int i=0; i<N; i++) {
if(result[i]) {
System.out.print(number[i] + " ");
}
}
System.out.println();
}
for(int i=start; i<N; i++) {
result[i]=true;
dfs(i+1, depth+1);
result[i] = false;
}
}
}
사실 이 코드는 참고한 블로그에서 거의 똑같이 입력한 코드라 할 수 있다 ㅠ
하지만 이렇게 기록에 남겨야 나중에 또 공부를 할 수 있을 것 같아서 기록을 남긴다 ㅠ
언제쯤 이렇게 멋있는 코드를 만들 수 있을지 ㅠㅠ
참고 자료 : https://log-laboratory.tistory.com/118
[백준] 6603번 자바 로또
문제 출처 https://www.acmicpc.net/problem/6603 6)개의 수를 골라 집합 S를 만든 다음 그 수만 가지고 번호를 선택하는 것이다. 예를 들어, k=8, S={1,2,3,5,8,13,21,34}인 경우 이 집합 S에서 수를 고를 수 있..
log-laboratory.tistory.com