티스토리 뷰
문제는 위와 같으며 주어진 숫자를 가지고 중복 순열을 구하는 문제입니다. 중복 순열이기 때문에 해당 수를 선택했는지 아닌지 확인하지 않고 바로 처음부터 수를 확인하며 선택하고 선택한 수 배열에 추가합니다.
자바 코드는 다음과 같습니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);
int[] nums = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
Arrays.sort(nums);
System.out.println(permutation(nums, new int[n], n, m, 0));
}
public static StringBuilder permutation(int[] nums, int[] selected, int n, int m, int depth) {
StringBuilder sb = new StringBuilder();
if (depth == m) {
for (int i = 0; i < depth; i++) {
sb.append(selected[i]).append(" ");
}
return sb.append("\n");
}
for (int i = 0; i < n; i++) {
selected[depth] = nums[i];
sb.append(permutation(nums, selected, n, m, depth + 1));
}
return sb;
}
}
'알고리즘' 카테고리의 다른 글
[백준알고리즘] 15663 - N과 M (9) (0) | 2021.05.10 |
---|---|
[백준알고리즘] 15657 - N과 M (8) (0) | 2021.05.08 |
[백준알고리즘] 15655 - N과 M (6) (0) | 2021.05.08 |
[백준알고리즘] 15654 - N과 M (5) (0) | 2021.05.07 |
[백준알고리즘] 15652 - N과 M (4) (0) | 2021.05.06 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 수학
- sort
- ionic
- DFS
- CodeDeploy
- programmers
- Baekjoon
- permutation
- AWS
- 프로그래머스
- CodePipeline
- spring
- Algorithm
- Dynamic Programming
- SWIFT
- string
- map
- 순열
- Combination
- CodeCommit
- java
- cloudfront
- 소수
- EC2
- search
- ECR
- BFS
- 에라토스테네스의 체
- 조합
- array
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함