주차마다 배운 내용 정리
개념은 해당 페이지에 이어서 정리
코드 정리는 주차씩 블로그 만들어서 정리
알고있는 개념이여도 다 적어둘 것
5! = 5 x 4 x 3 x 2 x 1
---> n x n-1 x n-2 x n-3 x n-4
f(n) = n(n-1)
f(n-1) = (n-1)(n-2)
f(n-2) = (n-2)(n-3)
//코드
int fac(int n){
if(n==1) return 1;
return n * f(n-1);
}
0 + 1 + 1 + 2 + 3 = 5

//코드
int fivo(int n){
if(n==1 || n==0) return n;
return f(n-1) + f(n-1);
}
void hanoi(int n, char from, char temp, char to){
if(n==1) printf("원판 1을 %c -> %c로 이동\n", from, to); // 마지막 넣는 것
else{
hanoi(n-1, from, to, temp);
printf("%d를 %c -> %c로 이동\n", n-1, from, to);
hanoi(n-1, temp, from, to);
}
}
int main(){
hanoi(4, 'A', 'B','C');
return 0;
}
순서와 상관 있게 뽑는 것 :: 순열
순서와 상관없이 뽑는 것 :: 조합
정렬이 되어 있어야함.
next_permutation(시작, 끝); :: 오름차순
prev_permutation(시작, 끝); :: 내림차순
[공식]
nPr : n! / (n-r)!
n : 몇개중에
r : 몇개 뽑을 건지
//코드
vector<int> v = {2,1,3};
int a[] = {1,2,3};
sort(v.begin(), v.end());
sort(&a[0], &a[0]+3);
next_permutation(v.begin(), v.end());
next_permutation(&a[0], &a[0]+3);
next_permutation(a, a+3);
기본순열
//코드
next_permutation(v.begin(), v.end());
재귀함수로 만드는 순열
이해가 안된다면, 꼭 그림 그려서 파악해보기
int make_permutation(int n, int r, int depth){
if(r==depth) return;
for(int i=depth; i<n;i++){
swap(v[r], v[depth]);
make_permutation(n, r, depth+1);
swap(v[r], v[depth]);
}
return;
}
