---
tags: 文華社課
---
> 文華高中電腦研究社29th
> 編輯 : 黃昱凱
> 宣傳 : 平時練C++ㄉ小號 https://www.instagram.com/l.o.l.i.c.o.n._learning_cpp/
# C++從零入門 Level 2
在本章主要會面向複習和綜合運用
新的東西只有副函式,在第五堂時會為大家講解
所以可以隨時回去複習喔~
> 目錄: [C++從零入門](/G0dWqZR7RIOwrK6gK0tEZg)
接下來我們會對常見題目做講解~
(吸收了大家的意見,把題目難度降低了~~)
## 回顧: 賦值運算、輸出/入、運算子
:::success
- [ ] [例題: d049中華民國萬歲!](https://zerojudge.tw/ShowProblem?problemid=d049)
:::
這題並不是很困難,只是題幹中的:1912 ≤ y ≤ 2147483647
可能讓你有點不知道它想說甚麼
其實當我們宣告一個int的變數時
他的範圍就被固定了喔!
如下:
```cpp
int x; //則x一定要在-2,147,483,648 至2,147,483,647內
```
簡單來說,這題的輸入**不會超過int的範圍**,可以放心使用int宣告變數
那如果超過了呢~?
如果輸入範圍外的值,將會導致**溢位(overflow)**
簡單來說,它會壞掉,不可以放這麼大的東西進去(?

(太小也不行喔w)
常見的型別和範圍如表:
| 型別 | 範圍 |
|:-------------:|:-------------------------------:|
| int | -2,147,483,648 至 2,147,483,647 |
| bool | true(1) or false(0) |
| float | 10^-38^~10^38^ |
| double | 10^-308^~10^308^ |
| long long int | 約 -9.22 * 10^18^~9.22 * 10^18^ |
### 複習:
#### 輸出/入:
```cpp
int x; //宣告
cin >> x; //輸入
cout << x; //輸出
```
#### 賦值運算(=):
```cpp
int x;
x = 5; // 把等號右邊的內容丟到左邊
```
#### 運算子
```cpp
int x = 10;// 舉例
x = x + 5;// x += 5,x = 15
x = x - 5;// x -= 5,x = 0
x = x * 5;// x *= 5,x = 25
x = x / 5;// x /= 5,x = 2
x = x % 5;// x %= 5,x = 10 / 5的餘數 = 0
```
:::success
- [ ] [類題: d060 還要等多久啊?](https://zerojudge.tw/ShowProblem?problemid=d060)
- if 可用可不用,看尼484邏輯鬼才w
:::
## 回顧: 條件
```cpp
if (條件){
程式;(條件成立時執行)
}else{
程式;(條件不成立時執行)
}
```
## 回顧: 迴圈、陣列
### 複習:
#### 迴圈:
```cpp
for(int i = 0;i < n;i ++){
程式;//執行n次
}
```
```cpp
while(n > 0){
程式;//執行n次
n --;
}
```
#### 陣列
```cpp
int a[n];//宣告有n個空間的int型別陣列
a[x] = 0;//將0存入a[x]
```
:::success
- [ ] [例題: d065 三人行必有我師](https://zerojudge.tw/ShowProblem?problemid=d065)
:::
:::success
- [ ] [例題: d074 電腦教室](https://zerojudge.tw/ShowProblem?problemid=d074)
- 可不用陣列
:::
## 綜合運用
:::warning
- [ ] [進階: a215 明明愛數數](https://zerojudge.tw/ShowProblem?problemid=a215)
- 可不用陣列,其他上述技巧幾乎都會用到
:::
:::danger
- [ ] [APCS大考題: g595: 1. 修補圍籬](https://zerojudge.tw/ShowProblem?problemid=g595)
:::
:::info
這裡就是第四次社課的結尾拉~
有問題都可以提問或私訊我喔~
我會的話都會盡量回答
段考數學考炸的
蘿莉控
副社_黃昱凱 敬上
:::
-=-=-=-=-=-=-=-=-=-=-=-=--=-=--=-=-=-
:::info
-=第五次社課起點=-
:::
## 副函式
副函式是**位於主函式**外,具有特定功能、操作特定作業的程式碼區塊
舉例:
```cpp=
#include<bits/stdc++.h>
using namespace std;
void say_hi(int x){ //副函式
for(int i = 0;i < x;i ++){
cout << "hi" <<endl;
}
}
int main(){ //主函式
int n;
cin >> n;
say_hi(n);
return 0;
}
```
:::success
輸入:
~~~
5
~~~
結果:
~~~
hi
hi
hi
hi
hi
~~~
:::
這是一個會輸出hi的程式,宣告副函式say_hi()
其中**小括弧中的int x稱為參數**
這個副函式可以在其他地方被**呼叫**,得到參數後執行
參數對於副函式不是必要的,也可以不設定
當然也可以在自己的函式中呼叫自己,也就是等等會提到的**遞迴**
而副函式是可以有型別的,如int、bool、char等等...
**void表示這個副函式不回傳值** 直接使用**return後面不加東西**
其他如int、bool、char等等 **return後面要加上回傳的值**
例子:
```cpp=
#include<bits/stdc++.h>
using namespace std;
int X2(int x){ //副函式
return x * 2;
}
int main(){ //主函式
int n;
cin >> n;
cout << X2(n) << endl;
}
```
呼叫這個程式會幫你把數字乘2
:::success
輸入:
~~~
5
~~~
結果:
~~~
10
~~~
:::
還記得很久以前的這題嗎w~?
請你試著用副函式寫看看~
:::success
- [ ] [例題:a002 簡易加法](https://zerojudge.tw/ShowProblem?problemid=a002)
tip:你可能會需要在副函式中放兩個參數,類似下面這樣
~~~
int sum(int x, int y){
}
~~~
:::
## 遞迴
其實我們剛剛已經有提到什麼是遞迴了~
就是在**函式中呼叫自己的函式**
但如果沒有**終止條件**將會沒完沒了
可能出現RTE(Run Time Error)或TLE(Time Limit Exceeded)
而且結果將無法計算
遞迴例子:
```cpp=
#include<bits/stdc++.h>
using namespace std;
int X2(int x){ //副函式
if (x > 1000) return x;//終止條件
return X2(x * 2);
}
int main(){ //主函式
cout << X2(2) << endl;
}
```
呼叫這個程式會幫你把2乘2
再把2乘2 乘2
再把2乘2乘2 乘2
再把2乘2乘2乘2 乘2
...
直到它大於1000就終止並回傳
:::success
結果:
~~~
1024
~~~
:::
:::success
- [ ] [例題:e357: 遞迴函數練習](https://zerojudge.tw/ShowProblem?problemid=e357)
:::
:::warning
- [ ] [進階題d487: Order's computation process](https://zerojudge.tw/ShowProblem?problemid=d487)
請使用遞迴計算乘階
:::
> 目錄: [C++從零入門](/G0dWqZR7RIOwrK6gK0tEZg)
> 下一章: [C++從零入門Level 3-1](https://hackmd.io/@lolicon5208/HkH-6b8jt)
## 參考解答
先想想再偷看喔w
### d049:
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int x;
cin >> x;
cout << x - 1911 <<endl;
return 0;
}
```
### d060:
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int x;
cin >> x;
cout << (85 - x) % 60 << endl;
return 0;
}
```
### d065
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[3];
for(int i = 0;i < 3;i ++){
cin >> a[i];
}
sort(a, a+3);
cout << a[2] <<endl;
}
```
### d074
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[15], n;
cin >> n;
for(int i = 0;i < n;i ++){
cin >> a[i];
}
sort(a, a+n);
cout << a[n-1] <<endl;
}
```
### a215:
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m;
while(cin >> n >> m){
int cnt = 1, total = n;
while(total <= m){
total += n + cnt;
cnt ++;
}
cout << cnt <<endl;
}
return 0;
}
```
### g595:
0x3f約等於10^9^約等於1e9
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main(){
int cost = 0;
int n, a[105];
cin >> n;
memset(a, 0x3f, sizeof(a));
for(int i = 1;i <= n;i ++){
cin >> a[i];
}
for(int i = 1;i <= n;i ++){
if (a[i] == 0) cost += min(a[i-1], a[i+1]);
}
cout << cost <<endl;
}
```
### a002
```cpp=
#include<bits/stdc++.h>
using namespace std;
int sum(int x, int y){ //副函式
return x + y;
}
int main(){ //主函式
int a, b;
cin >> a >> b;
cout << sum(a,b) << endl;
}
```
### e357
```cpp=
#include<bits/stdc++.h>
using namespace std;
int n;
int f(int x){
if (x == 1) return 1;
else if (x % 2 == 0) return f(x / 2);
else return f(x-1) + f(x+1);
}
int main(){
cin >> n;
cout << f(n) <<endl;
}
```
### d487
```cpp=
#include<bits/stdc++.h>
using namespace std;
int n;
long long total = 1;
void recursion(int x)
{
if (x <= 0) return;
if (x == 1) cout << x <<" ";
else cout << x <<" * ";
total *= x;
recursion(x-1);
}
int main()
{
while (cin >> n)
{
total = 1;
cout << n << "! = ";
recursion(n);
if (n == 0) cout << "1 ";
cout <<"= "<< total <<endl;
}
}
```