Recursion: Fibonacci sequence, recursive implementation of exponential enumeration, recursive implementation of permutation enumeration

Recursion: O(2^n)

        call yourself

Examples and code templates:

Fibonacci sequence

Enter an integer n and find the nth term of the Fibonacci sequence.

Assuming starting from 0, the 0th item is 0.

data range

0≤n≤39

sample

输入整数 n=5 

返回 5
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int Fibonacci(int n){
		if(n==0) return 0;
        if(n==1) return 1;
	    if(n==2) return 1;
	    return Fibonacci(n-1)+Fibonacci(n-2);
}
int main(){
	int n;
	cin>>n;
	cout<<Fibonacci(n)<<endl;
	return 0;
} 

O(n*2^n)

Recursive implementation of exponential enumeration

Randomly select any number of n integers from 1∼n, and output all possible options.

input format

Enter an integer n.

output format

Output one scheme per line.

Numbers in the same line must be arranged in ascending order, and two adjacent numbers are separated by exactly one space.

For schemes that do not choose any number, output a blank line.

This question has a custom validator (SPJ), and the order between the lines (different schemes) is arbitrary.

data range

1≤n≤15

Input sample:

3

Sample output:


3
2
2 3
1
1 3
1 2
1 2 3

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=15;
int n;
int st[N];
void dfs(int u){
	if(u==n){
		for(int i=0;i<n;i++){
			if(st[i]==1)
				cout<<i+1<<" ";
		}
		cout<<endl;
		return ;
	}
	st[u]=2;
	dfs(u+1);
	st[u]=0;
	 
	st[u]=1;
	dfs(u+1);
	st[u]=0;
}

int main(){
	cin>>n;
	dfs(0);	
	return 0;
}

Recursive implementation of permutation enumeration

Arrange n integers from 1 to n in a row and randomly shuffle the order, and output all possible orders.

input format

an integer n.

output format

Output all solutions in ascending order, 1 per line.

First, two adjacent numbers on the same line are separated by a space.

Secondly, for two different rows, compare the numbers corresponding to the subscripts one by one, and the one with the smaller lexicographical order is ranked first.

data range

1≤n≤9

Input sample:

3

Sample output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

 time complexity:

                A total of n layers of recursion:

                        The first layer is: O(n)

                           The second layer: there are n branches, each branch has a for loop, that is O(n*n)

                           The third layer: there are n*(n-1) branches, each branch has a for loop, that is, O(n*(n-1)*n)

                            ……

                           Layer n (leaf nodes): There are n! branches, each branch has a for loop, which is O(n!*n)

                        So the total time complexity is: n(1+n+n(n-1)+...+n!)

                        (1+n+n(n-1)+...+n!) is equivalent to: (n!+n!/1+n!/(1*2)+n!/(1*2*3) +…+n!/(n-1)!+n!/n!); First, this equation must be greater than n! and less than (n!+n!/1+n!/2+n!/4+ ...+n!/2^(n-1)+n!/2^n) that is 3n!

                        So the time complexity of this question is O(3n*n!), that is, O(n*n!)

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=10;
int n,state[N];
bool used[N];
void dfs(int u) {
	if(u>n) {
		for(int i=1; i<=n; i++)
			cout<<state[i]<<" ";
		cout<<endl;
		return ;
	}
	for(int i=1; i<=n; i++) {
		if(!used[i]) {
			state[u]=i;
			used[i]=true;
			dfs(u+1);
			state[u]=0;
			used[i]=false;
		}
	}
}
int main() {
	cin>>n;
	dfs(1);
	return 0;
}

Recursive implementation of composite enumeration

with score

Guess you like

Origin blog.csdn.net/m0_56501550/article/details/129738242