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
- sort
- 백준 16935
- 프로그래머스 숫자의 표현 java
- Math.ceil()
- time complexity
- 백준 15661
- 프로그래머스 연속된 수의 합 java
- java 반올림
- java
- 0으로 채우기
- Math.floor()
- 네트워크
- java 내림
- 알고리즘
- 백준 14391
- 자바
- Algorithm
- 코딩테스트
- 백준 11723
- java 올림
- 프로그래머스 도둑질 java
- mysql
- 백준 16927
- 프로그래머스 네트워크 java
- 백준 4375
- 프로그래머스 옹알이 java
- Codility
- Arrays
- 백준 18290
Archives
- Today
- Total
취미처럼
[백준] 15654번 N과 M (5) 본문
https://www.acmicpc.net/problem/15654
15654번: N과 M (5)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
자기자신은 방문 처리하고, 미리 저장한 arr 에서 해당 인덱스의 값을 ans 배열에 저장하여 출력
import java.util.*;
import java.io.*;
public class Main {
public static int N, M;
public static int[] ans;
public static int[] arr;
public static boolean[] visit;
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];
visit = new boolean[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 ++) {
if(!visit[i]) {
visit[i] = true;
ans[depth] = arr[i];
dfs(depth + 1);
visit[i] = false;
}
}
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 15656번 N과 M (7) (0) | 2021.03.03 |
---|---|
[백준] 15655번 N과 M (6) (0) | 2021.03.03 |
[백준] 15652번 N과 M (4) (0) | 2021.03.02 |
[백준] 15651번 N과 M (3) (0) | 2021.03.02 |
[백준] 15650번 N과 M (2) (0) | 2021.03.02 |
Comments