Algorithm/개념 정리

[알고리즘] 순열 & 조합

반응형


[알고리즘] 순열 & 조합



조합


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * 
 * 조합
    4 2 (4C2)
    0 1 
    0 2 
    0 3 
    1 2 
    1 3 
    2 3 
 *
 */
 
public class comb {
 
    public static void comb(int[] set, int size, int n, int k, int index){
        
        if(k==0) {
            for (int i = 0; i < size; i++) {
                System.out.print(set[i] + " ");
            }
            System.out.println();
            return;
        }
        
        else if (n==index) return;
        
        set[size] = index;
        comb(set, size+1, n, k-1, index+1);
        comb(set, size, n, k, index+1);
    }
    
    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int[] set = new int[100];
        String[] str = new String[2];
        
        str = br.readLine().split(" ");
        
        int n = Integer.parseInt(str[0]);
        int k = Integer.parseInt(str[1]);
        
        comb(set, 0, n, k, 0);
    }
 
}
 
cs




중복 조합


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * 
 * 중복 조합
    4 2 (4C2)
    0 0 
    0 1 
    0 2 
    0 3 
    1 1 
    1 2 
    1 3 
    2 2 
    2 3 
    3 3 
 *
 */
public class rcomb {
    
    public static void rcomb(int[] set, int size, int n, int k, int index){
        
        if(k==0){
            for (int i = 0; i < size; i++) {
                System.out.print(set[i] + " ");
            }
            System.out.println();
            return;
        }
        if(n==index) return;
        
        set[size] = index;
        rcomb(set, size+1, n, k-1, index);
        rcomb(set, size, n, k, index+1);
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int set[] = new int[100];
        String[] str = new String[2];
        
        str = br.readLine().split(" ");
        
        int n = Integer.parseInt(str[0]);
        int k = Integer.parseInt(str[1]);
        
        rcomb(set, 0, n, k, 0);
    }
 
}
cs



순열


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * 
 * 순열
    4 2 (4P2)
    0 1 
    0 2 
    0 3 
    1 0 
    1 2 
    1 3 
    2 1 
    2 0 
    2 3 
    3 1 
    3 2 
    3 0 
 *
 */
 
public class perm {
 
    public static void swap(int[] set, int i, int index){
        int temp = set[i];
        set[i] = set[index];
        set[index] = temp;
    }
    
    public static void perm(int[] set, int size, int n, int k){
        
        if(size == k){
            for (int i = 0; i < size; i++) {
                System.out.print(set[i] + " ");
            }
            System.out.println();
            return;
        }
    
        for (int i = size; i < n; i++) {
            swap(set, i, size);
            perm(set, size+1, n, k);
            swap(set, i, size);
        }
    }
    
    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int[] set = new int[100];
        String[] str = new String[2];
        
        str = br.readLine().split(" ");
        
        int n = Integer.parseInt(str[0]);
        int k = Integer.parseInt(str[1]);
        
        for (int i = 0; i < n; i++) {
            set[i] = i;
        }
        
        perm(set, 0, n, k);
    }
 
}
cs



중복 순열


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * 
 * 중복순열
    4 2 (4P2)
    0 0 
    0 1 
    0 2 
    0 3 
    1 0 
    1 1 
    1 2 
    1 3 
    2 0 
    2 1 
    2 2 
    2 3 
    3 0 
    3 1 
    3 2 
    3 3 
 *
 */
public class rperm {
 
    public static void rperm(int[] set, int size, int n, int k) {
        
        if(size==k) {
            for (int i = 0; i < size; i++) {
                System.out.print(set[i] + " ");
            }
            System.out.println();
            return;
        }
        
        for (int i = 0; i < n; i++) {
            set[size] = i;
            rperm(set, size+1, n, k);
        }
    }
    
    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int set[] = new int[100];
        String str[] = new String[2];
        
        str = br.readLine().split(" ");
        
        int n = Integer.parseInt(str[0]);
        int k = Integer.parseInt(str[1]);
        
        rperm(set, 0, n, k);
        
    }
 
}
cs



반응형