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)
|
'Programming Language > Python' 카테고리의 다른 글
[Django] 우분투 18.04 LTS Django 설치 (0) | 2019.11.03 |
---|---|
파이썬 10진수 n진수로 변환 (0) | 2019.10.12 |
Python itertools를 이용한 순열과 조합 (combinations, permutations) (0) | 2019.10.09 |
Python String copy (0) | 2019.10.09 |
Python Dictionary Max, Min Key, Value(파이썬 딕셔너리, 사전 최대 최소 구하기) (0) | 2019.10.05 |