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

N과 M (3)

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

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

 

15651번: N과 M (3)

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

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{
  
  static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
  static int[] result = 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=1; i<=N; i++) {
        result[x] = i;
        getResult(x+1);
      }
    }
  }
  
  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());
    
    result = new int[M];
    getResult(0);
    
    br.close();
    bw.flush();
    bw.close();
  }
  
}
cs

 

반응형

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

순열구하기  (0) 2021.09.22
N과 M (4)  (0) 2021.09.22
binary  (0) 2021.09.21
mountain  (0) 2021.09.21
1, 2, 3 더하기  (0) 2021.09.21