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
- 프로그래머스 연속된 수의 합 java
- 백준 16935
- 프로그래머스 옹알이 java
- Codility
- 백준 16927
- 프로그래머스 도둑질 java
- time complexity
- java 반올림
- 백준 18290
- java 올림
- 백준 17425
- java 내림
- Algorithm
- 백준 15661
- 프로그래머스 숫자의 표현 java
- 자바
- 프로그래머스 네트워크 java
- 백준 4375
- 알고리즘
- 네트워크
- 백준 14391
- 백준 11723
- mysql
- Arrays
- Math.ceil()
- 코딩테스트
- Math.floor()
- java
- 0으로 채우기
- sort
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