CS/알고리즘_문제풀이(자바)

N과 M (4)

Jedy_Kim 2021. 9. 22. 17:41
728x90

https://www.acmicpc.net/problem/15652

 

15652번: N과 M (4)

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

www.acmicpc.net

// 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
import java.io.*;
 
public class Main{
  
  static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
  static int[] result = null;
  static int[] visited = null;
  static int N;
  static int M;
  
  static void getResult(int x) throws Exception {
    if(x == M) {
      int myLen = result.length;
      for(int i=0; i<myLen; i++) {
        bw.write(String.valueOf(result[i]) + " ");
      }
      bw.newLine();
    } else {
      
      for(int i=0; i<N; i++) {
        if(visited[i] == 0) {
          result[x] = i+1;
          getResult(x+1);    
          visited[i] = 1;
          
          for(int j=i+1; j<N; j++) {
            visited[j] = 0;
          }
        }
         
        
      }
    }
  }
  
  public static void main(String[] args) throws Exception {
    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
    StringTokenizer st = new StringTokenizer(br.readLine());
    
    N = Integer.parseInt(st.nextToken());
    M = Integer.parseInt(st.nextToken());
    
    visited = new int[N+1];
    result = new int[M];
    getResult(0);
    
    br.close();
    bw.flush();
    bw.close();
  }
  
}
cs

 

반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

연결 요소의 개수  (0) 2021.09.23
순열구하기  (0) 2021.09.22
N과 M (3)  (0) 2021.09.22
binary  (0) 2021.09.21
mountain  (0) 2021.09.21