## 迴圈、陣列與vector
#### 講師:東兔角 aka 大二最廢人類
---
課程簡介
<hr style = "background:red;">
1. 迴圈
2. 陣列
3. vector
<hr style = "background:black;">
---
什麼是迴圈?
為什麼需要迴圈?
----
Ctrl C + Ctrl V ?

----
## 迴圈
迴圈是一段在程式中只出現一次
但可能會連續執行多次的程式碼
----
## 關於迴圈你需要會的東西
1. While迴圈
2. For迴圈
3. 讓迴圈中斷、跳過的酷東西
----
While迴圈
當條件達成,就做指定動作

----
While迴圈code範例
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
bool CCUPC_is_good = true;
while(CCUPC_is_good){ //判斷式
//執行內容
cout << "Of course!\n";
}
return 0;
}
```
----
TLE? 不會自己結束?

----
手動停止無窮迴圈

----
如何改善?
調整式?
設定一個停止的值?

----
while迴圈做5次code範例
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
int CCUPC_is_good = 0;
while(CCUPC_is_good < 5){ //判斷式
//執行內容
cout << "Of course!\n";
CCUPC_is_good++; //調整式
}
return 0;
}
```
----
do while迴圈
先做指定動作,如條件仍然達成,重複做指定動作

----
do while迴圈code範例
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
int CCUPC_is_good = 0;
do{
//先執行
cout << "Of course!\n";
CCUPC_is_good++;
}while(CCUPC_is_good < 5); //再判斷,要加分號!
}
```
----
For迴圈
給定一個初始式,當條件達成
就做指定的動作,最後對指定的東西做調整。

----
for迴圈code範例
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
for(int i = 0; i < 5; i++){
cout << "Of course!\n";
}
return 0;
}
```
----
For迴圈的應用
 
----
我能不能在迴圈裡面加迴圈,在迴圈裡面再加迴圈,再加迴圈,再加迴圈,再加迴圈,再加迴圈...

----
巢狀迴圈範例 - 99乘法表
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
for(int i = 1; i < 10; i++){
for(int j = 1; j < 10; j++){
cout << i << " * " << j << " = " << i * j << "\n";
}
}
return 0;
}
```
----
## 練習時間
A - 萬年星星樹
----
break;
讓迴圈提早結束的酷東西
(只針對迴圈)
----
break; code範例+執行結果
 
----
break; 只跳出一層迴圈
 
----
continue;
迴圈遇到continue會無條件跳過continue下方所有程式碼,直接進行下一次循環

----
continue; for迴圈執行結果
 
----
continue; while無窮迴圈
 
----
continue; while迴圈執行結果
 
----
什麼是EOF?
為什麼需要EOF?
----
EOF(End of file)
EOF代表文件的結尾
當cin讀到文件的結尾時會回傳
EOF(通常為-1)
手動輸入:
Windows : ctrl+z
Mac or linux : ctrl+d
----
手動輸入EOF範例
 
----
I/O優化
大量輸出、入的情況下,cin, cout會變得緩慢
需要使用特殊語法加速
使用後嚴禁與printf, scanf混合使用
也不要用endl換行
詳細原理可參考:
https://cp.wiwiho.me/io-optimize/
----
I/O優化code範例
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
//I/O優化
ios_base::sync_with_stdio(0);
cin.tie(0);
return 0;
}
```
----
## 練習時間
B - 我們與被當的距離
---
什麼是陣列?
為什麼需要陣列?
----
為什麼需要陣列?
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
int a1, a2, a3, a4, a5, a6;
cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6;
cout << a1 << a2 << a3 << a4 << a5 << a6;
return 0;
}
```
----
## 關於陣列你需要知道的東西
1. 陣列宣告
2. 初始化與取值
3. RE(RUNTIME ERROR)
----
陣列宣告
宣告一個大小為10,名為array的陣列,
儲存的資料型態為int


----
不同的初始化方式

----
陣列取值改值
陣列的index從0開始
 
----
for迴圈初始化
 
----
二維陣列宣告


----
RUNTIME ERROR
在用陣列時常常會因為2件事情RE
1. 陣列開太大
2. 戳到沒被宣告的位子
----
RE - 陣列開太大
```cpp=
#include <bits/stdc++.h>
using namespace std;
int array[1000000000];
int main(){
int array[10000000];
return 0;
}
```
----
MAXSIZE
陣列大小取決於記憶體大小
Judge的int陣列限制
全域最多開到$10^8$
區域最多開到$10^6$
----
RE程式碼範例
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
int array[5] = {1, 2, 3, 4, 5};
cout << array[-1];
cout << array[60];
return 0;
}
```
----
RE程式碼範例
int arr[2][10]只有到arr[1][9]
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[2][10];
for(int i = 0; i <= 2; i++){ //超過上限
for(int j = 0; j <= 10; j++){ //超過上限
cout << arr[i][j] << " ";
}
cout << "\n";
}
return 0;
}
```
----
## 練習時間
C - 區間和
---
什麼是vector?
為什麼要用vector?
----
## vector
動態陣列 aka 陣列pro
能根據需求更改陣列大小
----
## 關於vector你需要會的東西
1. 宣告vector
2. vector語法
3. vector應用
----
宣告vector

1. vector : 告訴電腦你要宣告的是vector
2. int : 要裝的資料型態,可以改成long long, float, double, bool等不同的資料型態
3. vt : 這個vector的名稱
----
宣告vector code範例
```cpp=
#include <iostream>
#include <vector> //記得include標頭檔
using namespace std;
int main(){
//宣告一個裝int的vector
vector <int> vt;
//宣告一個大小為5,裝int的vector
vector <int> vt(5);
//宣告一個大小為5,裝int,每個元素都是10的vector
vector <int> vt(5, 10);
}
```
----
vector用起來跟陣列很像
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
vector <int> vt = {1, 2, 3};
int array[3] = {1, 2, 3};
//取值
cout << array[0] << " " << vt[0] << "\n";
//改值
array[1] = 5; //{1, 5, 3}
vt[1] = 5; //{1, 5, 3}
}
```
----
vector語法
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
vector <int> vt = {1, 2, 3, 4, 5};
cout << vt[0]; //讀取第一項的值
cout << vt.front(); //回傳第一項的值
cout << vt.back(); //回傳最後一項的值
vt.push_back(7); //新增元素到最尾端 {1, 2, 3, 4, 5, 7}
vt.pop_back(); //刪除最尾端的元素 {1, 2, 3, 4, 5}
return 0;
}
```
----
vector語法
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
vector <int> vt = {1, 2, 3, 4, 5};
vt.clear(); //清空所有元素,size歸0 { };
vt.empty(); //如果內部為空,回傳true,否則false
vt.size(); //回傳vector大小,這裡是0
vt.resize(10);//重設大小為10 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
vt.assign(2, 5); //重設大小為2且元素都為5 {5, 5}
return 0;
}
```
----
vector應用
利用push_back和pop_back完成先進後出
 
----
二維vector宣告方式
```cpp=
#include <bits/stdc++.h>
using namespace std;
int main(){
//宣告2 * 5空間,全部都是1000
vector <int> vec(5, 1000);
vector <vector<int>> vt(2, vec);
//沒有指定,單純宣告一個空的二維vector
vector <vector<int>> vt;
return 0;
}
```
----
## 練習時間
D - 最強之盾
----
## 練習時間
E - 組隊天賦點滿
---
課程結束 謝謝大家
{"title":"迴圈、陣列與vector","slideOptions":"{\"theme\":\"black\"}","description":"課程簡介","showTags":"false","contributors":"[{\"id\":\"4b76862e-a52d-47fe-aed1-d6345f04d8db\",\"add\":20342,\"del\":12624}]"}