• C++
  • 信奥【0518】下午1---基础算法:递归

  • @ 2024-5-18 15:29:56
本周主题:递归
用递归必须知道两个条件:
递归出口(终止递归的条件)
递归表达式(规律)

技巧:在递归中常常是将问题切割成两个部分(1和整体的思想),
这能够让我们快速找到递归表达式(规律)

image

image

作业: 1、递归求阶乘 image

2、递归求阶乘和 image

3、信奥一本通--递归算法---爬楼梯 image image

3 条评论

  • @ 2024-5-25 13:43:23
    #include<bits/stdc++.h>
    using namespace std;
    int jjxx[100];
    int jx(int n){
    	if(n==1){
    		jjxx[1]=1;
    		return 1;
    	}
    	else if (jjxx[n]!=0){
    		return jjxx[n];
    	}else{
    		jjxx[n]=n*jx(n-1);
    		return jjxx[n];
    	}
    }
    
    int main(){
    	int n,sum;
    	cin>>n;
    	for(int i=1;i<=n;i++)sum+=jx(i);
    	cout<<jx(n);
    	return 0;
    }
    
    • @ 2024-5-25 13:35:31
      #include<bits/stdc++.h>
      using namespace std;
      
      int jx(int n){
      	if(n==1)return 1;
      	else return n*jx(n-1);
      }
      
      int main(){
      	int n;
      	cin>>n;
      	cout<<jx(n);
      	return 0;
      }
      
      • @ 2024-5-25 12:30:17

        怎么搞啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

        • 1