본문 바로가기

프로그래밍

Permutation [Python, Java, C++]

Python

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
# N, M을 입력받는다.
 
N, M = map(int, input().strip().split())
# 숫자를 입력받는다.
LST = list(map(int, input().strip().split()))
# 숫자를 정렬한다.
LST.sort()
 
def myPrint(lst):
    for i in lst:
        print(i, end=" ")
    print()
 
# M 개가 나올때까지 Recursion. 수열이므로 Visited를 사용한다.
def permute(count,visited, current):
    if count ==M:
        myPrint(current)
    else:
        for i in range(N):
            if not visited[i]:
                current.append(LST[i])
                visited[i]= True
                permute(count+1, visited, current)
                current.pop()
                visited[i] = False
 
permute(0, [False]*N, [])
 
cs

Java

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
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
 
public class Main {
    static int N;
    static int M;
    static StringBuilder sb;
    static Stack<Integer> alreadyIn;
    static int[] element;
 
    public static void main(String[] args) throws Exception {
 
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
 
        element = new int[N];
        for(int i = 0; i < N; i++){
            element[i] = sc.nextInt();
        }
        Arrays.sort(element);
 
        sb = new StringBuilder();
        alreadyIn = new Stack<>();
 
        recur(0"");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(sb.toString().trim());
        bw.close();
    }
 
    public static void recur(int applyNum, String makingStr) {
        if (applyNum == M) {
            sb.append(makingStr.trim() + "\n");
        } else {
            ++applyNum;
            for (int i = 0; i < N; i++) {
                if (!alreadyIn.contains(element[i])) {
                    alreadyIn.push(element[i]);
                    recur(applyNum, makingStr + " " + element[i]);
                    alreadyIn.pop();
                }
            }
        }
    }
}
cs

C++

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
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<functional>
 
using namespace std;
 
int n, m;
vector<int> v;
int visited[8];
vector<int> result;
 
void fun(int count) {
    if (count == m) {
        for (int i = 0; i < m; i++) {
            printf("%d ", result[i]);
        }
        printf("\n");
    }
    else {
        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                visited[i] = 1;
                result.push_back(v[i]);
                fun(count + 1);
                result.pop_back();
                visited[i] = 0;
            }
        }    
    }
}
 
int main() {
    int temp;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        v.push_back(temp);
    }
    sort(v.begin(), v.end());
 
    fun(0);
 
    return 0;
}
cs