Notice
Recent Posts
Recent Comments
Link
개발 공부~
[백준 - 15652] N과 M (4) .java 본문
https://www.acmicpc.net/problem/15652
- (1) 순열
- (2) 조합
- (3) 중복 순열
(4) 중복이 허용되면서 오름차순(정확히는 비내림차순) 인 수열을 만드는 문제
즉, 시작점 중복 허용 -> 시작점을 i 로 하면 된다
작은 값을 배제하기 위해 시작점을 설정 -> 비내림차순 보장
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Main {
static int n;
static int m;
static int[] result;
static BufferedWriter bw;
public static void dfs(int depth, int start) throws IOException {
if(depth ==m) {
for(int i =0; i<m;i++) {
bw.write(result[i]+" ");
}
bw.newLine();
return;
}
for(int i =start; i<=n;i++) {
result[depth] = i;
dfs(depth+1,i);
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
bw = new BufferedWriter(new OutputStreamWriter(System.out));
n = sc.nextInt();
m = sc.nextInt();
result= new int[m];
dfs(0,1);
bw.flush();
bw.close();
}
}'코딩테스트 > 백준' 카테고리의 다른 글
| [백준 - 1759] 암호 만들기 .java (0) | 2025.11.16 |
|---|---|
| [백준 - 1182] 부분수열의 합 .java (0) | 2025.11.16 |
| [백준 - 15650] N과 M (2) .java (0) | 2025.11.16 |
| [백준 - 4796] 캠핑 .java (0) | 2025.11.15 |
| [백준 - 13305] 주유소 .java (0) | 2025.11.15 |