[Algorithm] 조합론(combinatorics)

2024. 4. 11. 10:17· Computer Science/Algorithm&DataStructure
목차
  1. ✨팩토리얼(factorial)
  2. ✨순열(permutation)
  3. ✨조합(combination)
728x90

✨팩토리얼(factorial)

그 수보다 작거나 같은 모든 양의 정수의 곱

 

python 코드
import sys

n=int(sys.stdin.readline().rstrip())
su=1
for i in range(1,n+1,1): su*=i
sys.stdout.write(str(su))

 

 

 

 

 

✨순열(permutation)

순서가 부여된 임의의 집합을 다른 순서로 뒤섞는 연산
n개의 요소에서 k개를 뽑는다.

 

itertools의 permutations코드
from itertools import permutations

n=[1,2,3,4]
k=2

print("개수:"+str(len(list(permutations(n,k)))))
for i in permutations(n,k):print(i, end="")

#결과
#개수: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)

유한 개의 원소에서 주어진 수만큼의 원소들을 고르는 방법
조합의 수는 이항 계수로 주어진다.

 

python 코드
import sys
def factorial(a):
    sm=1
    for i in range(1,a+1,1): sm*=i
    return(sm)

N,K=map(int,sys.stdin.readline().rstrip().split(" "))

sys.stdout.write(str(int(factorial(N)/(factorial(K)*factorial(N-K)))))

 

 

 

itertools의 combinations 코드
from itertools import combinations

n=[1,2,3,4]
k=2

print("개수:"+str(len(list(combinations(n,k)))))
for i in combinations(n,k):print(i, end="")

#결과
#개수:6
#(1, 2)(1, 3)(1, 4)(2, 3)(2, 4)(3, 4)

 

 

 

 

 

 

 

 

 

🎈참고자료

https://ko.wikipedia.org/wiki/%EC%88%9C%EC%97%B4

 

 

 

 

728x90

'Computer Science > Algorithm&DataStructure' 카테고리의 다른 글

[Algorithm] 합병정렬(merge sort)  (0) 2024.04.11
[Algorithm] 유클리드 호제법(Euclidean algorithm)  (0) 2024.04.10
[Algorithm] Set(집합)  (0) 2024.04.10
  1. ✨팩토리얼(factorial)
  2. ✨순열(permutation)
  3. ✨조합(combination)
'Computer Science/Algorithm&DataStructure' 카테고리의 다른 글
  • [Algorithm] 백트래킹(backtracking)
  • [Algorithm] 합병정렬(merge sort)
  • [Algorithm] 유클리드 호제법(Euclidean algorithm)
  • [Algorithm] Set(집합)
아사_
아사_
프로그래밍 공부한거 정리해두는 메모장 블로그
아사_
개발공부 블로그
아사_
전체
오늘
어제
  • 분류 전체보기 N
    • FrontEnd N
      • html
      • css
      • JavaScript
      • Node.js
      • React N
      • React Native
    • BackEnd
      • SpringBoot
      • FastAPI
      • PHP
      • Flask
      • supabase
    • Language
      • Python
      • JAVA
      • Kotlin
      • C++
    • Development Tools
      • AWS
      • GIT,GITHUB
      • Docker
      • 메시지 브로커
      • 기타 도구,플랫폼
    • Computer Science
      • 개발지식
      • Server&Network
      • Algorithm&DataStructure
      • Security
      • DataBase
      • OS
    • AI
    • 기타
      • 잡다
      • Android
      • 도서
    • 클론코딩
      • 생활코딩 Express.js
      • 점프 투 장고
      • 생활코딩 Node.js
    • 프로젝트 N
      • DevQuest N

인기 글

최근 글

250x250
hELLO · Designed By 정상우.v4.2.2
아사_
[Algorithm] 조합론(combinatorics)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.