算法知识-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;
}
相关推荐
Vacant Seat34 分钟前
贪心算法-跳跃游戏II
算法·游戏·贪心算法
夜松云43 分钟前
从对数变换到深度框架:逻辑回归与交叉熵的数学原理及PyTorch实战
pytorch·算法·逻辑回归·梯度下降·交叉熵·对数变换·sigmoid函数
八股文领域大手子1 小时前
深入浅出限流算法(三):追求极致精确的滑动日志
开发语言·数据结构·算法·leetcode·mybatis·哈希算法
新时代苦力工1 小时前
处理对象集合,输出Map<String, Map<String, List<MyObject>>>格式数据,无序组合键处理方法
java·数据结构·list
啊阿狸不会拉杆1 小时前
人工智能数学基础(一):人工智能与数学
人工智能·python·算法
一捌年1 小时前
java排序算法-计数排序
数据结构·算法·排序算法
freexyn2 小时前
Matlab自学笔记五十二:变量名称:检查变量名称是否存在或是否与关键字冲突
人工智能·笔记·算法·matlab
渭雨轻尘_学习计算机ing2 小时前
二叉树构建算法全解析
算法·程序员
乌鸦9443 小时前
《数据结构之美--二叉树》
数据结构·#二叉树
C语言魔术师4 小时前
70. 爬楼梯
算法·动态规划