Programming Language/Python
Python 순열(Permutation)
hackyu
2019. 10. 9. 07:08
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# 1 ~ n 까지의 숫자가 있을 때 나열할 수 있는 경우의 수 혹은 경우
def perm(a):
length = len(a)
if length == 1:
return [a]
else:
result = []
for i in a:
b = a.copy()
b.remove(i)
b.sort()
for j in perm(b):
j.insert(0, i)
if j not in result:
return result
if __name__ == "__main__":
num = int(input('1부터 n까지 자연수를 나열하는 순열을 구합니다. n 을 입력하세요 : '))
a = [i for i in range(1,num+1)]
c = perm(a)
for i in range(len(c)):
print(i , c[i])
|
|
2019.10.14 n개의 숫자에서 m개로 표현할 수 있는 경우의 수
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# m개의 숫자를 n개 표현
result = []
m, n = map(int, input().strip().split())
def solve(T):
if T>=n:
for i in result:
print(i, end="")
print("")
else:
for i in range(m):
alpha = chr(ord('a')+i)
if alpha not in result:
solve(T+1)
solve(0)
|
|