• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

pm배열

21.08.25 13:29 작성 조회수 120

0

pm배열 없이 ch를 이용해서 바로 답을 출력하도록 했는데 제 코드는 왜 오답인지 잘 모르겠습니다.

public class Ch8_6 {
    public static int n, m;
    public static int[] arr,ch;

    public static void DFS(int length) {
        if(length == m) {
            for(int i = 0; i < n; i++) {
                if(ch[i] == 1) {
                    System.out.print(arr[i] + " ");
                }
            }
            System.out.println();
        } else {
            for(int i = 0; i < n; i++) {
                if(ch[i] == 0) {
                    ch[i] = 1;
                    DFS(length + 1);
                    ch[i] = 0;
                }
            }
        }
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        m = sc.nextInt();
        arr = new int[n+1];
        ch = new int[n+1];

        for(int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        DFS(0);
    }
}

답변 1

답변을 작성해보세요.

0

안녕하세요^^

채점사이트는 공백문자까지 구분합니다.

1 2출력할 때 2뒤에 공백이 있으면 안됩니다.