⏱ 70 分钟 | 前置:第 10 课(函数定义、值传递)| 工具:VSCode + g++
| 时间 | 环节 |
|---|---|
| 0–5 分钟 | 复习第 10 课 + 作业讲评(重点讲组合数的复用和 swap 改错) |
| 5–17 分钟 | 知识点1:作用域与生命周期 |
| 17–27 分钟 | 知识点2:函数重载与默认参数 |
| 27–47 分钟 | 知识点3:递归思想与调用过程(本课重头戏) |
| 47–55 分钟 | 知识点4:汉诺塔演示 |
| 55–67 分钟 | 课堂练习 |
| 67–70 分钟 | 总结 + 布置作业 |
#include <iostream> using namespace std; int g = 100; // 全局变量:函数外定义,整个程序都认识它 void show() { cout << g << endl; // 100,任何函数都能用全局 // cout << x; // 错!x 是 main 的局部变量,show 看不见 } int main() { int x = 1; // 局部变量:main 里出生,main 结束就销毁 { int x = 2; // 新块里的同名新变量,把外层 x "遮住"了 cout << x << endl; // 2 } // 里层 x 在这里销毁 cout << x << endl; // 1 ← 外层 x 毫发无损 for (int i = 0; i < 3; i++) { } // cout << i; // 错!i 只活在 for 里(第 5 课埋的伏笔) show(); return 0; }
| 种类 | 定义位置 | 作用域(哪能用) | 生命周期(活多久) |
|---|---|---|---|
| 局部变量 | 函数/块内(含形参) | 从定义处到所在块结束 | 进块出生,出块销毁 |
| 全局变量 | 所有函数外 | 定义处之后的所有函数 | 程序启动到结束 |
const int MAX = 100; 这类常量(常量没人能改,安全)。#include <iostream>
using namespace std;
int maxOf(int a, int b) { // 版本1:两个 int
return a > b ? a : b;
}
double maxOf(double a, double b) { // 版本2:两个 double
return a > b ? a : b;
}
int maxOf(int a, int b, int c) { // 版本3:三个 int
return maxOf(maxOf(a, b), c);
}
int main() {
cout << maxOf(3, 7) << endl; // 7 → 编译器按参数挑版本1
cout << maxOf(2.5, 1.5) << endl; // 2.5 → 版本2
cout << maxOf(3, 7, 5) << endl; // 7 → 版本3
return 0;
}int f(int) 和 double f(int) 并存直接编译报错maxOf,不用背 maxInt / maxDouble / max3Intvoid printLine(int n = 20, char c = '=') { // 声明/定义时给默认值
for (int i = 0; i < n; i++) cout << c;
cout << endl;
}
int main() {
printLine(); // 20 个 =
printLine(30); // 30 个 =(实参从左往右填)
printLine(30, '-'); // 30 个 -
// printLine('-'); // 错!想只给 c 不行,'-' 会填给 n
return 0;
}void f(int a, int b = 1, int c = 2) ✔;void f(int a = 1, int b) ✗(左边有默认右边没有,编译报错)。原因:实参从左往右填,中间不能跳洞。#include <iostream>
using namespace std;
int fact(int n) {
if (n <= 1) return 1; // ① 终止条件:不再往下问,直接给答案
return n * fact(n - 1); // ② 向终止靠近:n 每层减 1
}
int main() {
cout << fact(4) << endl; // 24
return 0;
}展开(一层层往下问):
回归(答案一层层传回):fact(1)=1 → fact(2)=2×1=2 → fact(3)=3×2=6 → fact(4)=4×6=24。
fact(4)——程序疯狂递归后崩溃(stack overflow)。改回来,强调先写终止条件,再写递归式的肌肉记忆。#include <iostream>
using namespace std;
int fib(int n) {
if (n <= 2) return 1; // 第 1、2 项都是 1
return fib(n - 1) + fib(n - 2); // 前两项之和
}
int main() {
for (int i = 1; i <= 10; i++) {
cout << fib(i) << " "; // 1 1 2 3 5 8 13 21 34 55
}
cout << endl;
return 0;
}#include <iostream> using namespace std; // 把 n 个盘从 from 借助 mid 移到 to void hanoi(int n, char from, char mid, char to) { if (n == 1) { // 终止:一个盘直接搬 cout << from << " -> " << to << endl; return; } hanoi(n - 1, from, to, mid); // ① n-1 个:from → mid cout << from << " -> " << to << endl; // ② 最大盘:from → to hanoi(n - 1, mid, from, to); // ③ n-1 个:mid → to } int main() { int n; cout << "请输入盘子数:"; cin >> n; hanoi(n, 'A', 'B', 'C'); return 0; }
n=3 输出 7 步;总步数 = 2ⁿ - 1(n=10 → 1023 步,n=64 → 宇宙年龄都搬不完,这就是那个"世界末日"传说)。人想破头也写不出的移动序列,递归三行搞定——这就是"相信小一号的自己"的威力。
recsum.cpp —— 写 int sum(int n):sum(n) = n + sum(n-1),终止 sum(1)=1。输入 n 输出和。#include <iostream>
using namespace std;
int sum(int n) {
if (n <= 1) return 1;
return n + sum(n - 1);
}
int main() {
int n;
cout << "请输入 n:";
cin >> n;
cout << "和 = " << sum(n) << endl;
return 0;
}area.cpp —— 用重载写两个 area:一个参数(圆,double r)返回 πr²;两个参数(长方形,double a, double b)返回 ab。main 里分别调用验证。
#include <iostream>
using namespace std;
const double PI = 3.14159;
double area(double r) { // 一个参数 → 圆
return PI * r * r;
}
double area(double a, double b) { // 两个参数 → 长方形
return a * b;
}
int main() {
cout << "半径 2 的圆面积 = " << area(2.0) << endl;
cout << "3×4 长方形面积 = " << area(3.0, 4.0) << endl;
return 0;
}fibn.cpp —— 输入 n(1~40),输出斐波那契第 n 项(递归版)。再用循环版验证同一个 n,对比 n=40 时两者的速度差异。
#include <iostream>
using namespace std;
int fib(int n) {
if (n <= 2) return 1;
return fib(n - 1) + fib(n - 2);
}
int fibLoop(int n) {
int a = 1, b = 1;
for (int i = 3; i <= n; i++) {
int c = a + b;
a = b;
b = c;
}
return b;
}
int main() {
int n;
cout << "请输入 n(1~40):";
cin >> n;
cout << "递归版 = " << fib(n) << endl;
cout << "循环版 = " << fibLoop(n) << endl;
return 0;
}digitsum.cpp —— 写 int digitSum(int n):digitSum(n) = n%10 + digitSum(n/10),终止 n==0 返回 0。输入正整数输出各位和。12345→15,7→7。
#include <iostream>
using namespace std;
int digitSum(int n) {
if (n == 0) return 0;
return n % 10 + digitSum(n / 10); // 砍个位,第 5 课 while 版的递归化身
}
int main() {
int n;
cout << "请输入正整数:";
cin >> n;
cout << "各位数字之和 = " << digitSum(n) << endl;
return 0;
}printrev.cpp —— 写 void printRev(int n),把 n 的各位数字倒着打印(123 → 321,直接输出字符,不构造反转数)。#include <iostream>
using namespace std;
void printRev(int n) {
if (n == 0) return;
cout << n % 10; // 本层先打印自己的个位
printRev(n / 10); // 剩下的高位交给下一层
}
int main() {
int n;
cout << "请输入正整数:";
cin >> n;
printRev(n);
cout << endl;
return 0;
}hanoicount.cpp —— 写 long long hanoiCount(int n) 返回移动步数:hanoiCount(1)=1,hanoiCount(n)=2×hanoiCount(n-1)+1。输入 n(1~63)输出步数。3→7,10→1023,63→9223372036854775807(正好是 long long 最大值!)。选做:给课堂 hanoi 加全局计数器验证步数一致。
#include <iostream>
using namespace std;
long long hanoiCount(int n) {
if (n == 1) return 1;
return 2 * hanoiCount(n - 1) + 1; // 两次 n-1 搬运 + 最大盘一步
}
int main() {
int n;
cout << "请输入盘子数(1~63):";
cin >> n;
cout << "需要 " << hanoiCount(n) << " 步" << endl;
return 0;
}#include <iostream>
#include <string>
using namespace std;
int fact(int n) {
return n * fact(n - 1);
}
void greet(string name = "同学", int times) {
for (int i = 0; i < times; i++) {
cout << "你好," << name << endl;
}
}
double scale(int x) { return x * 2.0; }
int scale(int x) { return x * 2; }
int main() {
cout << fact(5) << endl;
greet(3);
return 0;
}fact 没有终止条件,无限递归直到栈溢出崩溃——加 if (n <= 1) return 1;;② greet 的默认参数不在最右(name 有默认而 times 没有),编译报错——把参数顺序换成 (int times, string name = "同学") 或给 times 也加默认值;③ 两个 scale 只有返回类型不同,不构成重载,编译报错——删掉一个或改参数类型;④ greet(3) 想传次数,但按原签名 3 会填给第一个参数——修正签名后应写 greet(3);(times 在前)或 greet("小明", 3);,调用要和参数顺序对齐。