---
# System prepended metadata

title: 第三堂
tags: [資料結構筆記]

---

---
title: 第三堂
tags: 資料結構筆記
---

#### 更大的 rand()

```cpp
num = (num<<16)|rand();
```

### 遞迴演算法

- 直接遞迴
- 間接遞迴
- 終止條件

#### 階層
```cpp=
#include <bits.stdc++.h>
using namespace std;

int factorial(int n){
    if(n==1)return 1;
    return n*factorial(n-1);
}

int main(){
    int n;
    cin>>n;
    cout<<factorial(n)<<endl;
}
```

#### Towers of Hanoi

```cpp=
#include <bits/stdc++.h>
using namespace std;

void Hanoi(int n,char a,char b,char c){
    if(n==1){
        cout<<"move disk from "<<a<<" to "<<c<<endl;
    }
    else{
        Hanoi(n-1,a,c,b);
        Hanoi(1,a,b,c);
        Hanoi(n-1,b,a,c);
    }
}

int main(){
    int n;cin>>n;
    Hanoi(n,'A','B','C');
}
```
