백준 6603번 : 로또

반응형

etc-image-0
https://www.acmicpc.net/problem/6603

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    StringBuilder sb = new StringBuilder();
    int n = 1;
    int[] numArray;
    int[] result;
    boolean[] visit;

    public void solution() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(n!=0){
            StringTokenizer st = new StringTokenizer(br.readLine());
            n = Integer.parseInt(st.nextToken());
            numArray = new int[n];
            result = new int[6];
            visit = new boolean[n];
            for(int i=0; i<n; i++){
                numArray[i] = Integer.parseInt(st.nextToken());
            }
            dfs(0,0);
            sb.append("\n");
        }
        System.out.println(sb);
    }

    public void dfs(int index, int depth){
        if(depth==6){
            for(int a : result){
                sb.append(a).append(" ");
            }
            sb.append("\n");
            return;
        }
        for(int i = index; i<n; i++){
                result[depth] = numArray[i];
                dfs(i+1, depth + 1);
        }
    }





    public static void main(String args[]) throws IOException {
        new Main().solution();
    }
   }

dfs를 활용한다. 결과는 6가지의 숫자다. 그 6 가지의 숫자는 오름차순이어야만 하기 때문에

현재의 값보다 항상 큰 수가 올 수 있도록 인덱스(i)+1을 해준다. depth가 6일때 저장하고, 출력한다.

반응형

'개발 > 알고리즘' 카테고리의 다른 글

백준 11723번 : 집합  (0) 2023.01.25
백준 문제풀이 입력과 출력  (0) 2023.01.22
백준 10971번 : 외판원 순회 2  (0) 2023.01.20
백준 10819번 : 차이를 최대로  (0) 2023.01.18
백준 10974번 : 모든 순열  (0) 2023.01.18