백준 6603 자바

손찬호·2024년 7월 30일
0

알고리즘

목록 보기
85/91

풀이 아이디어

재귀를 사용해서 가능한 모든 조합의 수를 탐색한다.
재귀를 야무지게 사용하니까 풀렸다.

풀이 코드

import java.io.*;
public class _6603 {
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static int[] inputNumbers;
    static int[] selected = new int[6];
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            String[] input = br.readLine().split(" ");
            if(input[0].equals("0")) break;
            
            // 맨 처음 수 k는 개수를 나타낸다. 
            int k = Integer.parseInt(input[0]);
            inputNumbers = new int[k];
            for(int i=0;i<k;i++){
                inputNumbers[i] = Integer.parseInt(input[i+1]);
            }

            // k개의 수 중에서 6개를 뽑는다.
            combination(0, 0);
            bw.write("\n");
        }
        bw.flush(); 
        bw.close();
    }

    static void combination (int start, int size) throws IOException {
        if (size==6){
            for(int i=0;i<6;i++){
                bw.write(selected[i]+" ");
            }
            bw.write("\n");
            return ;
        }
        for(int i=start;i<inputNumbers.length;i++){
            selected[size]=inputNumbers[i];
            combination(i+1, size+1);
        }
    }    
}
profile
매일 1%씩 성장하려는 주니어 개발자입니다.

0개의 댓글

관련 채용 정보