当前位置: 首页 > article >正文

算法知识-14-递归

递归的概念

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

图示说明:

递归图示
代码示例:

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 递归版台阶问题升级

#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时,上一次的除数即为最大公约数。这种方法效率较高,适用于较大的数。

#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个数

#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青蛙跳

#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兔子繁殖问题

#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递归求阶乘

#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递归版台阶问题

#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累加和

#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用递归实现输出

#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倒序数

#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;
}

http://www.kler.cn/a/414098.html

相关文章:

  • VTK的基本概念(一)
  • 【Qt】QSettings类实现配置信息长期保存(掉电不擦除)
  • 【开发商城系统】
  • Redis开发03:常见的Redis命令
  • pandas 数据分析流程
  • 鸿蒙HarmonyOS学习笔记(6)
  • Opencv+ROS实现摄像头读取处理画面信息
  • Vue3.0里为什么要用 Proxy API 替代 defineProperty API ?
  • OGRE 3D----5. OGRE和QML事件交互
  • HarmonyOS-初级(二)
  • Spring Boot整合EasyExcel
  • WPS文字学习计划与策略
  • 在受限网络环境下的 Docker 部署实践 —— Strapi 和 Appsmith 的解决之道20241128
  • python-解决一元一次方程
  • Unix和Linux之异同
  • 【后端开发】Go语言编程实践,Goroutines和Channels,基于共享变量的并发,反射与底层编程
  • 【人工智能】从零构建一个文本分类器:用Python和TF-IDF实现
  • 软件工程——期末复习(适用于sdut)
  • vue-baidu-map基本使用
  • webpack 项目访问静态资源