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
- 0으로 채우기
- 프로그래머스 도둑질 java
- sort
- 백준 11723
- 자바
- Algorithm
- java 반올림
- 알고리즘
- 프로그래머스 네트워크 java
- 네트워크
- 백준 15661
- java
- 백준 16927
- java 올림
- mysql
- 백준 4375
- time complexity
- 백준 16935
- 백준 18290
- Math.floor()
- 프로그래머스 숫자의 표현 java
- 프로그래머스 연속된 수의 합 java
- Math.ceil()
- 코딩테스트
- Codility
- 프로그래머스 옹알이 java
- java 내림
- Arrays
- 백준 14391
Archives
- Today
- Total
취미처럼
[백준] 15657번 N과 M (8) 본문
https://www.acmicpc.net/problem/15657
15657번: N과 M (8)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
중복도 있으니 방문체크 로직은 필요 없고, start만 증가시켜 준다.
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, 0);
System.out.println(sb);
}
private static void dfs(int start, int depth) {
if (depth == M) {
for (int val : ans) {
sb.append(val).append(" ");
}
sb.append("\n");
return;
}
for (int i = start; i < N; i++) {
ans[depth] = arr[i];
dfs(i, depth + 1);
}
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 1759번 암호 만들기 (0) | 2021.03.03 |
---|---|
[백준] 18290번 NM과 K (1) (0) | 2021.03.03 |
[백준] 15656번 N과 M (7) (0) | 2021.03.03 |
[백준] 15655번 N과 M (6) (0) | 2021.03.03 |
[백준] 15654번 N과 M (5) (0) | 2021.03.03 |
Comments