## 🧑‍🏫 一維陣列與基本迴圈 ### 🎯 本週目標: 1. 一維陣列的宣告、存取與初始化 2. if、else if、else 3. 邏輯運算子(&&, ||, !) 4. for 迴圈走訪陣列 5. 用程式讀取多筆資料並做基本運算(如總和、平均) ### 📦 陣列基本用法 ```cpp #include <iostream> using namespace std; int main() { int scores[5] = {90, 85, 78, 92, 88}; for (int i = 0; i < 5; i++) { cout << "第 " << i + 1 << " 位分數:" << scores[i] << endl; } return 0; } ``` ### 🧠 條件判斷語法 ```cpp if (條件) { // 條件成立時執行 } else if (另一個條件) { // 第二個條件成立時執行 } else { // 其他情況 } ``` #### 範例 ```cpp 🧪 範例:判斷奇偶數 ``` ### 🔗 比較與邏輯運算子 |類型| 運算子 | 意思 | |----| -------- | -------- | |比較| == | 等於 | || != | 不等於 | || > | 大於 | || < | 小於 | || >= | 大於等於 | || <= | 小於等於 | |邏輯| && |且| ||\|\| |或| | | ! | 非 | ### 🔁 for 迴圈結構 ```cpp for (int i = 0; i < n; i++) { // 重複執行的程式區塊 } ``` #### 範例:1 到 n 的總和 ```cpp int n, sum = 0; cin >> n; for (int i = 1; i <= n; i++) { sum += i; } cout << "總和為:" << sum << endl; ``` ### 🧪 練習題 ✅ [ZeroJudge A003: 兩光法師占卜術](https://zerojudge.tw/ShowProblem?problemid=a003) ✅ [ZeroJudge A104: 排序](https://zerojudge.tw/ShowProblem?problemid=a104) ✅ (進階) [ZeroJudge c680: 電腦閱卷1](https://zerojudge.tw/ShowProblem?problemid=c680)