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