Python, 순열(Permutation)과 조합(Combination)
1, 2, 3, 4번의 카드 중 2개를 뽑아 나열하는 경우의 수
# 순열(Permutation) - 순서 고려
case=0
for i in range(1, 5):
for j in range(1, 5):
if i != j:
print(i, j)
case+=1
print("total:", case) # total: 12
case=0
for i in range(1, 5):
for j in range(1, 5):
if i != j:
print(i, j)
case+=1
print("total:", case) # total: 12
#1 2
#1 3
#1 4
#2 1
#2 3
#2 4
#3 1
#3 2
#3 4
#4 1
#4 2
#4 3
# 조합(Combination) - 순서 고려하지 않음
case=0
for i in range(1, 5):
for j in range(i+1, 5):
print(i, j)
case+=1
print("total:", case) # totla: 6
case=0
for i in range(1, 5):
for j in range(i+1, 5):
print(i, j)
case+=1
print("total:", case) # totla: 6
#1 2
#1 3
#1 4
#2 3
#2 4
#3 4
댓글
댓글 쓰기