250x250
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 |
Tags
- N과M
- 자바 코테
- 정올 1620
- 주사위굴리기2
- 정보처리기사
- Bfs와DFS
- 백준13458
- 23288
- 중복순열
- 코테준비
- 볼륨 만들기
- 코테
- 알고리즘개념
- 완탐
- 삼성역테
- 백준
- 순열
- 에라토스테네스의채
- 완전탐색
- 백준2251
- 전화번호속의암호
- 알고리즘
- D드라이브생성
- 자바
- 중복조합
- 파티션 크기 조정
- BFS
- 재귀함수
- 백준15652
- java
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 15655번 N과 M (6) - Java 본문
728x90
https://www.acmicpc.net/problem/15655
조합 문제임은 자명하다.
여기서 오름차순으로 출력하라고 되어있으므로 애초에 배열을 오름차순 정렬시켜준다.
public class Main {
static int N,M;
static int[] arr;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine()," ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new int[N];
st = new StringTokenizer(in.readLine(), " ");
for(int i=0;i<N;i++) {
arr[i] = Integer.parseInt(st.nextToken());
} // 배열 초기화
// 수열은 사전순으로 증가해야하므로 배열을 초기에 오름차순으로 정렬
Arrays.sort(arr);
// 중복 x , 조합
comb(0, new int[M], 0);
System.out.print(sb);
}
static void comb(int cnt, int[] selected, int startIdx) {
if(cnt == M) {
for(int i=0;i<cnt;i++) {
sb.append(selected[i]).append(' ');
}
sb.append('\n');
return;
}
for(int i=startIdx;i<N;i++) {
selected[cnt]= arr[i];
comb(cnt+1, selected, i+1);
}
}
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 15656번 N과 M (8) - Java (0) | 2021.07.13 |
---|---|
[문제] 백준 15656번 N과 M (7) - Java (0) | 2021.07.13 |
[문제] 백준 15654번 N과 M (5) - Java (0) | 2021.07.13 |
[문제] 백준 15652번 N과 M (4) -Java (0) | 2021.07.13 |
[문제] 백준 15651번 N과 M (3) -Java (0) | 2021.07.13 |
Comments