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

N과 M (1)

Jedy_Kim 2021. 9. 15. 11:40
728x90

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

 

15649번: N과 M (1)

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

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
import java.util.*;
import java.io.*;
 
public class Main{ 
  public static void getResult(int x, int N, int M, int[]result, int[]check) {
    
    if(x >= M) {
      for(int i=0; i<result.length; i++) {
        System.out.print(result[i] + " ");
      }
      System.out.println();
    } else {
      
      for(int i=1; i<=N; i++) {
        if(check[i] == 0) {
          check[i]  = 1;
          result[x] = i; 
          getResult(x+1, N, M, result, check);
          check[i]  = 0;
        }
        
      }
      
    }
    
  }
  
  public static void main(String[] args) throws Exception {
 
    // Please Enter Your Code Here
    BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());
    
    int N = Integer.parseInt(st.nextToken());
    int M = Integer.parseInt(st.nextToken());
    
    int[] result = new int[M];
    int[] check  = new int[N+1];
    getResult(0, N, M, result, check);
    
  }
}
cs

 

반응형

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

ABCDE  (0) 2021.09.15
N과 M (2)  (0) 2021.09.15
문자열 포함관계 조사  (0) 2021.09.14
문자열 정렬  (0) 2021.09.14
큰 자릿수 곱셈  (0) 2021.09.14