-
[Algorithm] 중복순열Algorithm 2023. 4. 10. 13:46
중복순열
중복 순열은 순열과 마찬가지로 n개 중에 r개를 순서에 상관 있게 뽑는데, 중복을 허락하여 뽑는 것을 말한다.
구현
순열 코드에서 visited 체크 안 하면 중복순열!
package Algorithm; import java.util.Arrays; public class 중복순열 { static int[] arr = {1,2,3}; static int[] res = new int[3]; public static void main(String[] args) { perm(0); } private static void perm(int idx){ if(idx == arr.length){ //출력 System.out.println(Arrays.toString(res)); return; } for(int i=0; i<arr.length; i++){ res[idx] = arr[i]; perm(idx+1); } } }
결과
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 2, 1]
[1, 2, 2]
[1, 2, 3]
[1, 3, 1]
[1, 3, 2]
[1, 3, 3]
[2, 1, 1]
[2, 1, 2]
[2, 1, 3]
[2, 2, 1]
[2, 2, 2]
[2, 2, 3]
[2, 3, 1]
[2, 3, 2]
[2, 3, 3]
[3, 1, 1]
[3, 1, 2]
[3, 1, 3]
[3, 2, 1]
[3, 2, 2]
[3, 2, 3]
[3, 3, 1]
[3, 3, 2]
[3, 3, 3]'Algorithm' 카테고리의 다른 글
[Algorithm] BFS (0) 2023.04.25 [Algorithm] 부분집합 (0) 2023.04.10 [Algorithm] 중복 조합 (0) 2023.04.10 [Algorithm] 조합 (0) 2023.04.10 [Algorithm] 순열 (0) 2023.04.10