#include<iostream>using namespace std;
// 方法1:辗转相除法(欧几里得算法)intgcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// 方法2:递归实现intgcd_recursive(int a, int b) {
if (b == 0) return a;
return gcd_recursive(b, a % b);
}
intmain() {
int a = 48, b = 18;
cout << a << " 和 " << b << " 的最大公约数:";
cout << gcd(a, b) << endl; // 6// 应用:化简分数int numerator = 48, denominator = 18;
int divisor = gcd(numerator, denominator);
cout << numerator << "/" << denominator << " = ";
cout << numerator/divisor << "/" << denominator/divisor << endl; // 8/3return0;
}
求最小公倍数(LCM)
最小公倍数:两个或多个整数公有的倍数中最小的一个。
#include<iostream>using namespace std;
intgcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// 利用公式:LCM(a,b) = a * b / GCD(a,b)intlcm(int a, int b) {
return a / gcd(a, b) * b; // 先除后乘,避免溢出
}
intmain() {
int a = 12, b = 18;
cout << a << " 和 " << b << " 的最小公倍数:";
cout << lcm(a, b) << endl; // 36return0;
}
🎮 互动实验:数学计算器
输入数字,体验各种数学算法
输入数字后点击"计算"...
🛠️ 经典数学序列
斐波那契数列
斐波那契数列:0, 1, 1, 2, 3, 5, 8, 13, 21...,每个数是前两个数之和。
#include<iostream>using namespace std;
// 方法1:迭代法(推荐)long longfibonacci_iterative(int n) {
if (n <= 0) return0;
if (n == 1) return1;
long long a = 0, b = 1, c;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
// 方法2:递归法(简洁但效率低)long longfibonacci_recursive(int n) {
if (n <= 0) return0;
if (n == 1) return1;
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
}
intmain() {
// 输出前20个斐波那契数
cout << "前20个斐波那契数:";
for (int i = 0; i < 20; i++) {
cout << fibonacci_iterative(i) << " ";
}
cout << endl;
// 计算第50个斐波那契数
cout << "第50个斐波那契数:" << fibonacci_iterative(50) << endl;
return0;
}
#include<iostream>using namespace std;
// 方法1:迭代法long longfactorial_iterative(int n) {
long long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// 方法2:递归法long longfactorial_recursive(int n) {
if (n <= 1) return1;
return n * factorial_recursive(n - 1);
}
intmain() {
// 计算1到10的阶乘for (int i = 1; i <= 10; i++) {
cout << i << "! = " << factorial_iterative(i) << endl;
}
return0;
}
判断完全数
完全数:所有真因子(除了自身以外的因子)之和等于自身的数。如6 = 1 + 2 + 3。
#include<iostream>using namespace std;
boolisPerfect(int n) {
if (n <= 1) returnfalse;
int sum = 1; // 1是所有数的因子for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
sum += i;
if (i != n / i) { // 避免重复加平方根
sum += n / i;
}
}
}
return sum == n;
}
intmain() {
cout << "10000以内的完全数:";
for (int i = 2; i <= 10000; i++) {
if (isPerfect(i)) {
cout << i << " ";
}
}
cout << endl; // 6 28 496 8128return0;
}
🎯 数学序列生成器
选择要生成的数学序列
选择序列类型后查看结果...
🚀 高级数学应用
实际应用:简单的计算器
#include<iostream>#include<cmath>using namespace std;
intmain() {
double a, b;
char op;
cout << "=== 简易计算器 ===" << endl;
cout << "请输入:数字1 运算符 数字2" << endl;
cout << "支持的运算符:+ - * / ^ s(平方根)" << endl;
cout << "示例:5 + 3 或 s 16" << endl;
cout << "输入:";
cin >> op;
if (op == 's' || op == 'S') {
// 平方根运算
cin >> a;
if (a < 0) {
cout << "错误:不能对负数开平方" << endl;
} else {
cout << "sqrt(" << a << ") = " << sqrt(a) << endl;
}
} else {
// 二元运算
cin >> a >> b;
switch (op) {
case'+':
cout << a << " + " << b << " = " << (a + b) << endl;
break;
case'-':
cout << a << " - " << b << " = " << (a - b) << endl;
break;
case'*':
cout << a << " * " << b << " = " << (a * b) << endl;
break;
case'/':
if (b == 0) {
cout << "错误:除数不能为0" << endl;
} else {
cout << a << " / " << b << " = " << (a / b) << endl;
}
break;
case'^':
cout << a << " ^ " << b << " = " << pow(a, b) << endl;
break;
default:
cout << "不支持的运算符" << endl;
}
}
return0;
}
数学在编程中的应用场景
数学知识在实际编程中有广泛应用:
🎮 游戏开发:碰撞检测、物理引擎、图形变换
📊 数据分析:统计计算、概率分布、回归分析
🔐 密码学:质数、模运算、加密算法
🤖 人工智能:线性代数、微积分、优化算法
🌐 图形学:几何计算、矩阵变换、光照模型
💰 金融计算:复利计算、风险评估、期权定价
性能优化技巧
// 技巧1:预计算并缓存结果constint MAX_N = 1000;
long long fib_cache[MAX_N];
long longfibonacci_memo(int n) {
if (n <= 1) return n;
if (fib_cache[n] != 0) return fib_cache[n]; // 已计算过
fib_cache[n] = fibonacci_memo(n - 1) + fibonacci_memo(n - 2);
return fib_cache[n];
}
// 技巧2:使用位运算优化// 判断奇偶:n % 2 等价于 n & 1// 乘以2:n * 2 等价于 n << 1// 除以2:n / 2 等价于 n >> 1// 技巧3:减少重复计算double result = sqrt(x); // 只计算一次for (int i = 0; i < n; i++) {
// 使用result,而不是每次都调用sqrt(x)
}
📝 实战练习
📝 理解测试
以下代码的输出是什么?
#include<iostream>using namespace std;
intgcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
intmain() {
cout << gcd(36, 24) << endl;
return0;
}