Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준 17425
- 백준 18290
- time complexity
- 프로그래머스 옹알이 java
- java
- 0으로 채우기
- 백준 15661
- Arrays
- Algorithm
- 프로그래머스 연속된 수의 합 java
- mysql
- 백준 4375
- java 올림
- java 반올림
- Codility
- 프로그래머스 숫자의 표현 java
- 백준 11723
- Math.ceil()
- 백준 16935
- 자바
- 네트워크
- sort
- 알고리즘
- 코딩테스트
- java 내림
- Math.floor()
- 프로그래머스 네트워크 java
- 백준 14391
- 백준 16927
- 프로그래머스 도둑질 java
Archives
- Today
- Total
취미처럼
[백준] 15656번 N과 M (7) 본문
https://www.acmicpc.net/problem/15656
15656번: N과 M (7)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
중복을 제거할 필요 없으므로 방문 체크 로직 제거
import java.util.*;
import java.io.*;
public class Main {
public static int N, M;
public static int[] ans;
public static int[] arr;
public static StringBuffer sb = new StringBuffer();
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()); // 자연수
ans = new int[M];
arr = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
dfs(0);
System.out.println(sb);
}
private static void dfs(int depth) {
if(depth == M) {
for(int val : ans) {
sb.append(val).append(" ");
}
sb.append("\n");
return;
}
for(int i = 0 ; i < N; i ++) {
ans[depth] = arr[i];
dfs(depth + 1);
}
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 18290번 NM과 K (1) (0) | 2021.03.03 |
---|---|
[백준] 15657번 N과 M (8) (0) | 2021.03.03 |
[백준] 15655번 N과 M (6) (0) | 2021.03.03 |
[백준] 15654번 N과 M (5) (0) | 2021.03.03 |
[백준] 15652번 N과 M (4) (0) | 2021.03.02 |
Comments