算法知识-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;
}
相关推荐
计算机小白一个6 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^6 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
大数据追光猿8 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!8 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉8 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生8 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴8 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing8 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
qy发大财8 小时前
加油站(力扣134)
算法·leetcode·职场和发展
王老师青少年编程8 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛