ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 재귀 - 백준 15657 N과 M(8) - 중복 있는 조합
    자료구조와 알고리즘/문제풀기 2024. 4. 28. 15:37

    N과 M (8) 

     
    시간 제한메모리 제한제출정답맞힌 사람정답 비율
    1 초 512 MB 26145 21251 18204 81.261%

    문제

    N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다.

    • N개의 자연수 중에서 M개를 고른 수열
    • 같은 수를 여러 번 골라도 된다.
    • 고른 수열은 비내림차순이어야 한다.
      • 길이가 K인 수열 A가 A1 ≤ A2 ≤ ... ≤ AK-1 ≤ AK를 만족하면, 비내림차순이라고 한다.

    입력

    첫째 줄에 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)

    둘째 줄에 N개의 수가 주어진다. 입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.

    출력

    한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.

    수열은 사전 순으로 증가하는 순서로 출력해야 한다.

    예제 입력 1 복사

    3 1
    4 5 2
    

    예제 출력 1 복사

    2
    4
    5
    

    예제 입력 2 복사

    4 2
    9 8 7 1
    

    예제 출력 2 복사

    1 1
    1 7
    1 8
    1 9
    7 7
    7 8
    7 9
    8 8
    8 9
    9 9
    

    예제 입력 3 복사

    4 4
    1231 1232 1233 1234
    

    예제 출력 3 복사

    1231 1231 1231 1231
    1231 1231 1231 1232
    1231 1231 1231 1233
    1231 1231 1231 1234
    1231 1231 1232 1232
    1231 1231 1232 1233
    1231 1231 1232 1234
    1231 1231 1233 1233
    1231 1231 1233 1234
    1231 1231 1234 1234
    1231 1232 1232 1232
    1231 1232 1232 1233
    1231 1232 1232 1234
    1231 1232 1233 1233
    1231 1232 1233 1234
    1231 1232 1234 1234
    1231 1233 1233 1233
    1231 1233 1233 1234
    1231 1233 1234 1234
    1231 1234 1234 1234
    1232 1232 1232 1232
    1232 1232 1232 1233
    1232 1232 1232 1234
    1232 1232 1233 1233
    1232 1232 1233 1234
    1232 1232 1234 1234
    1232 1233 1233 1233
    1232 1233 1233 1234
    1232 1233 1234 1234
    1232 1234 1234 1234
    1233 1233 1233 1233
    1233 1233 1233 1234
    1233 1233 1234 1234
    1233 1234 1234 1234
    1234 1234 1234 1234

     

    풀이 

    import java.io.*;
    import java.util.Arrays;
    
    public class Main {
    
        static int n;
        static int m;
        static int[] numbers;
        static int[] output;
    
        static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
        public static void main(String[] args) throws IOException {
    
    
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
            String[] input = br.readLine().split(" ");
            n = Integer.parseInt(input[0]);
            m = Integer.parseInt(input[1]);
            output = new int[n];
            numbers  = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    
            Arrays.sort(numbers);
    
            RecursiveCombinationWithItSelf(0,0);
    
            bw.flush();
            bw.close();
    
    
        }
    
        private static void RecursiveCombinationWithItSelf(int depth,int start) throws IOException {
    
            if(depth == m){
    
                for(int i=0; i<m;i++){
                    bw.write(output[i]+" ");
                }
                bw.write("\n");
    
            }else{
    
                for(int i=start; i<n; i++){
                    output[depth] = numbers[i];
                    RecursiveCombinationWithItSelf(depth+1,i);
                }
    
            }
    
        }
    
    }

     

    - 중복있는 조합을 만들기 위해 중복이 발생할 수 있도록 재귀 호출시 start를 for문의 i와 맞춰주었고, 중복 여부를 위한 check검사도 하지 않았다.  

Designed by Tistory.