算法知识-14-递归

递归的概念

递归是一种直接或间接调用自身函数或者方法的算法思想。它将一个复杂的问题分解为一个与原问题相似但规模更小的子问题,通过不断地重复这个过程,直到子问题变得简单到可以直接求解。

图示说明:

代码示例:

cpp 复制代码
int i = 0;
void fun() {
    cout << "进入下一层" << endl;
    i++;
    if (i < 5)
        fun();
    cout << "回归上一层" << endl;
}

fun()函数内部首先输出 "进入下一层",然后变量i自增。

然后条件判断if (i < 5),如果条件成立,则递归调用fun()函数。

最后,当递归调用结束后,输出 "回归上一层"。

cout << "进入下一层" << endl;和i++;这两句代码在每次递归进入下一层时执行。

cout << "回归上一层" << endl;这行代码在每次递归返回上一层时执行。

if (i < 5)用来限制递进的条件,被称为递归出口,当i不再小于 5 时,递归停止。

下面的问题不是都要用递归完成,可以使用递推

5177 递归版台阶问题升级

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int a(int x){
    if(x==1||x==2) return x;
    if(x==3) return 4;
    return a(x-1)+a(x-2)+a(x-3);
}
int n;
int main(){
    cin>>n;
    cout<<a(n);
    return 0;
}

3103土地分割

就是找最大公约数

可以使用辗转相除法‌:将较大的数除以较小的数,将余数作为新的被除数,反复进行除法运算,直到余数为0时,上一次的除数即为最大公约数。这种方法效率较高,适用于较大的数。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
long long n,m;
long long gcd(long long a,long long b){
    return b==0?a:gcd(b,a%b);
}
int main(){
	cin>>n>>m;
    cout<<gcd(n,m);
	return 0;
}

3102输出序列第n个数

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int f(int x){
	if(x==1) return 1;
    return f(x-1)+2;
}
int n;
int main(){
    cin>>n;
    cout<<f(n);
	return 0;
}

3101青蛙跳

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int f(int x){
    if(x==1||x==2) return x;
	return f(x-1)+f(x-2);
}
int n;
int main(){
    cin>>n;
    cout<<f(n);
	return 0;
}

3100兔子繁殖问题

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int f(int x){
    if(x==1||x==2) return 1;
	return f(x-1)+f(x-2);
}
int main(){
    cout<<f(12);
	return 0;
}

3099递归求阶乘

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int f(int x){
    if(x==1) return 1;
	return f(x-1)*x;
}
int n;
int main(){
    cin>>n;
    cout<<f(n);
	return 0;
}

3098递归版台阶问题

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int f(int x){
    if(x==1||x==2) return x;
	return f(x-1)+f(x-2);
}
int n;
int main(){
    cin>>n;
    cout<<f(n);
	return 0;
}

3097累加和

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int f(int x){
    if(x==1) return 1;
	return f(x-1)+x;
}
int main(){
    cout<<f(10);
	return 0;
}

3096用递归实现输出

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
void a(int n){
	if(n>1) a(n-1);
	cout<<n<<' ';
} 
int main(){
    a(10);
	return 0;
}

1674倒序数

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
void f(int x){
    int t=x%10;
    cout<<t;
    if(x>9) f(x/10);
}
int n;
int main(){
    cin>>n;
    f(n);
	return 0;
}
相关推荐
思捻如枫32 分钟前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
嘉陵妹妹37 分钟前
深度优先算法学习
学习·算法·深度优先
GalaxyPokemon1 小时前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展
小猫咪怎么会有坏心思呢1 小时前
华为OD机考 - 水仙花数 Ⅰ(2025B卷 100分)
数据结构·链表·华为od
hn小菜鸡2 小时前
LeetCode 1356.根据数字二进制下1的数目排序
数据结构·算法·leetcode
zhuiQiuMX2 小时前
分享今天做的力扣SQL题
sql·算法·leetcode
music&movie3 小时前
算法工程师认知水平要求总结
人工智能·算法
laocui14 小时前
Σ∆ 数字滤波
人工智能·算法
yzx9910134 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
全栈凯哥5 小时前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表