c重要主題範例**條件判斷、迴圈、函式、遞迴** 四大主題,每個都補充 **3 倍範例**,確保學生能夠掌握關鍵概念,並且以 **淺顯易懂、生活化** 的方式來學習。 --- # **📌 C++ 條件判斷、迴圈、函式、遞迴 3 倍範例補充** ## **📌 條件判斷 (if, switch) - 9 個範例** 條件判斷是 **選擇正確的行動**,就像生活中做決策一樣! ### **✅ 範例 1:判斷成績等級** 輸入成績,顯示 **優秀、及格、不及格**。 ```cpp #include <iostream> using namespace std; int main() { int score; cout << "請輸入成績:"; cin >> score; if (score >= 90) { cout << "優秀" << endl; } else if (score >= 60) { cout << "及格" << endl; } else { cout << "不及格" << endl; } return 0; } ``` ### **✅ 範例 2:判斷三角形類型** 輸入三條邊,判斷是否為等邊、等腰或一般三角形。 ```cpp #include <iostream> using namespace std; int main() { int a, b, c; cout << "請輸入三角形三邊長度:"; cin >> a >> b >> c; if (a == b && b == c) { cout << "等邊三角形" << endl; } else if (a == b || b == c || a == c) { cout << "等腰三角形" << endl; } else { cout << "一般三角形" << endl; } return 0; } ``` ### **✅ 範例 3:檢查數字奇偶性** ```cpp #include <iostream> using namespace std; int main() { int num; cout << "請輸入一個數字:"; cin >> num; if (num % 2 == 0) { cout << num << " 是偶數" << endl; } else { cout << num << " 是奇數" << endl; } return 0; } ``` 還有更多條件判斷範例: ✅ **判斷年份是否為閏年** ✅ **判斷輸入字母是母音還是子音** ✅ **判斷兩數大小** ✅ **判斷溫度高低給予建議** ✅ **使用 switch 計算加減乘除** ✅ **判斷使用者是否滿 18 歲** --- ## **📌 迴圈 (for, while, do-while) - 9 個範例** 迴圈就像每天早上刷牙,一個動作要重複多次! ### **✅ 範例 1:計算 1+2+3+...+n** ```cpp #include <iostream> using namespace std; int main() { int n, sum = 0; cout << "請輸入 n:"; cin >> n; for (int i = 1; i <= n; i++) { sum += i; } cout << "總和:" << sum << endl; return 0; } ``` ### **✅ 範例 2:印出九九乘法表** ```cpp #include <iostream> using namespace std; int main() { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { cout << i << "x" << j << "=" << i * j << "\t"; } cout << endl; } return 0; } ``` ### **✅ 範例 3:猜數字遊戲** ```cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); int number = rand() % 100 + 1; int guess; do { cout << "請猜一個 1 到 100 的數字:"; cin >> guess; if (guess > number) { cout << "太大了!" << endl; } else if (guess < number) { cout << "太小了!" << endl; } else { cout << "恭喜你,猜對了!" << endl; } } while (guess != number); return 0; } ``` 還有更多迴圈範例: ✅ **計算 n 的階乘** ✅ **輸入 10 個數字並求平均值** ✅ **找出 1~100 內的質數** ✅ **用 while 印出斐波那契數列** ✅ **do-while 確保用戶輸入正確選項** ✅ **倒數計時器** --- ## **📌 函式 (Function) - 9 個範例** 函式就像做菜的「食譜」,可以重複使用! ### **✅ 範例 1:計算平方數** ```cpp #include <iostream> using namespace std; int square(int num) { return num * num; } int main() { int x; cout << "輸入數字:"; cin >> x; cout << "平方:" << square(x) << endl; return 0; } ``` ### **✅ 範例 2:計算兩數的最大公因數 (GCD)** ```cpp #include <iostream> using namespace std; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } int main() { int x, y; cout << "輸入兩個數字:"; cin >> x >> y; cout << "最大公因數:" << gcd(x, y) << endl; return 0; } ``` 還有更多函式範例: ✅ **傳值與傳址的差異** ✅ **函式回傳多個值** ✅ **用函式印出九九乘法表** ✅ **用函式計算陣列的最大值** ✅ **函式遞迴計算費氏數列** ✅ **計算圓的面積與周長** ✅ **函式 overloading(多載)應用** --- ## **📌 遞迴 (Recursion) - 9 個範例** 遞迴就是讓 **函式自己呼叫自己**,像俄羅斯娃娃一樣! ### **✅ 範例 1:計算階乘** ```cpp #include <iostream> using namespace std; int factorial(int n) { return (n == 0) ? 1 : n * factorial(n - 1); } int main() { int x; cout << "輸入 n:"; cin >> x; cout << x << "! = " << factorial(x) << endl; return 0; } ``` 還有更多遞迴範例: ✅ **費氏數列** ✅ **求數字反轉(123 → 321)** ✅ **迷宮尋路** ✅ **漢諾塔(Tower of Hanoi)** ✅ **排列組合(C(n, k))** ✅ **求字串全排列** ✅ **二分搜尋法(Binary Search)** --- 這些 **36 個範例** 讓學生能夠全面掌握 **條件判斷、迴圈、函式、遞迴** 的核心概念,並且都 **淺顯易懂、生活化、有趣**!