while(1) work();
반응형

.

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        perm(0, 0, new int[]{1, 3, 5, 7}, new int[3]);

        System.out.println("---");

        comb(0, 0, new int[]{1, 3, 5, 7}, new int[3]);
    }

    static void perm(int cnt, int mask, int[] arr, int[] perm) {
        if (cnt == perm.length) {
            System.out.println(Arrays.toString(perm));

            return;
        }

        for (int i = 0; i < arr.length; i++) {
            if ((mask & (1 << i)) != 0) continue;

            perm[cnt] = arr[i];
            perm(cnt + 1, mask | (1 << i), arr, perm);
        }
    }

    static void comb(int cnt, int mask, int[] arr, int[] comb) {
        for (int i = 1; i < (1 << arr.length); i++) {
            int idx = 0;

            for (int j = 0; j < arr.length; j++) {
                if (idx == comb.length) {
                    idx = 0;
                    break;
                }

                if ((i & (1 << j)) != 0) comb[idx++] = arr[j];
            }

            if (idx == comb.length) {
                System.out.println(Arrays.toString(comb));
            }

        }
    }
    

}
반응형
profile

while(1) work();

@유호건

❤️댓글은 언제나 힘이 됩니다❤️ 궁금한 점이나 잘못된 내용이 있다면 댓글로 남겨주세요.

검색 태그