본문 바로가기

알고리즘/재귀

[JAVA] 백준 6603번: 로또

문제

독일 로또는 {1, 2, ..., 49}에서 수 6개를 고른다.

로또 번호를 선택하는데 사용되는 가장 유명한 전략은 49가지 수 중 k(k>6)개의 수를 골라 집합 S를 만든 다음 그 수만 가지고 번호를 선택하는 것이다.

예를 들어, k=8, S={1,2,3,5,8,13,21,34}인 경우 이 집합 S에서 수를 고를 수 있는 경우의 수는 총 28가지이다. ([1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], ..., [3,5,8,13,21,34])

집합 S와 k가 주어졌을 때, 수를 고르는 모든 방법을 구하는 프로그램을 작성하시오.

입력

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있다. 첫 번째 수는 k (6 < k < 13)이고, 다음 k개 수는 집합 S에 포함되는 수이다. S의 원소는 오름차순으로 주어진다.

입력의 마지막 줄에는 0이 하나 주어진다. 

출력

각 테스트 케이스마다 수를 고르는 모든 방법을 출력한다. 이때, 사전 순으로 출력한다.

각 테스트 케이스 사이에는 빈 줄을 하나 출력한다.

 

 

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class baekjoon6603 {
    static int n;
    static int[] info;
    static boolean[] visit;    
    static int[] res;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean isTrue = true;        
        while (isTrue) {
            n = sc.nextInt();
            info = new int[n];
            visit = new boolean[n];            
            res = new int[6];

            if (n == 0) {
                isTrue = false;
            }
            for (int i = 0; i < n; i++) {
                info[i] = sc.nextInt();
            }
            permuatation(0, 0, res);
            System.out.println();
        }
    }
    static void permuatation(int point, int index, int[] res){
        if (index == 6) {
            for (int i : res) {
                System.out.print(i+" ");
            }
            System.out.println();
            return;
        }
        for (int i = point; i < info.length; i++) {
            if (!visit[i]) {
                // 방문한 적이 없으면
                visit[i] = true;
                res[index] = info[i];
                permuatation(i, index+1, res);
                visit[i] = false;
            }
        }
    }
}

 

방문 여부를 확인 할 필요가 없다.

코드 수정

import java.util.Scanner;

public class baekjoon6603 {
    static int n;
    static int[] info;     
    static int[] res;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean isTrue = true;        
        while (isTrue) {
            n = sc.nextInt();
            info = new int[n];                      
            res = new int[6];

            if (n == 0) {
                isTrue = false;
            }
            for (int i = 0; i < n; i++) {
                info[i] = sc.nextInt();
            }
            permuatation(0, 0, res);
            System.out.println();
        }
    }
    static void permuatation(int point, int index, int[] res){
        if (index == 6) {
            for (int i : res) {
                System.out.print(i+" ");
            }
            System.out.println();
            return;
        }
        for (int i = point; i < info.length; i++) {            
            // 방문한 적이 없으면                
            res[index] = info[i];
            permuatation(i+1, index+1, res);                            
        }
    }
}

'알고리즘 > 재귀' 카테고리의 다른 글

백준 14501번: 퇴사  (0) 2021.07.31
[JAVA] 백준 16198번: 에너지 모으기  (0) 2021.07.24
[JAVA] 백준 14500번: 테트로미노  (0) 2021.07.22
[JAVA] 백준 1182번: 부분수열의 합  (0) 2021.07.21