Day 20 算法笔记之算法初步4.3 递归

目录

1.n的阶乘

2.fibonacci数列

3.全排列

4.8皇后

5.回溯版8皇后

6.吃糖果

7.数列

8.神奇的口袋


1.n的阶乘

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

int F(int n){
	if(n==0){
		return 1;
	}else{
		return F(n-1)*n;
	}
}


int main(){
	
	int n;
	scanf("%d",&n);
	printf("%d\n",F(n));
	
	return 0;
}

2.fibonacci数列

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

int F(int n){
	if(n<=1){
		return 1;
	}else{
		return F(n-1)+F(n-2);
	}
}


int main(){
	
	int n;
	scanf("%d",&n);
	printf("%d\n",F(n));
	
	return 0;
}

3.全排列

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

bool hashtable[1000] = {false};
int table[1000];

void f(int n, int index){
	
	if(index==n){
		for(int i=0;i<n;i++){
			printf("%d",table[i]);
		}
		printf("\n");
		return;
	}
	
	for(int i=1;i<n+1;i++){
		if(hashtable[i]==false){
			table[index] = i;
			hashtable[i] = true;
			f(n,index+1);
			hashtable[i] = false;
		}
	}
}


int main(){
	
	int n;
	scanf("%d",&n);
	f(n,0);
	
	return 0;
}

4.8皇后

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

bool hashtable[1000] = {false};
int table[1000];
int counts=0;

void f(int n, int index){
	
	if(index==n){
		bool flag = true;
		
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				if(abs(j-i)==abs(table[j]-table[i])){
					flag=false;
				}
			}
		}
		
		if(flag){
			counts++;
		}
		
		return;
	}
	
	for(int i=1;i<n+1;i++){
		if(hashtable[i]==false){
			table[index] = i;
			hashtable[i] = true;
			f(n,index+1);
			hashtable[i] = false;
		}
	}
}


int main(){
	
	int n;
	scanf("%d",&n);
	f(n,0);
	
	printf("%d\n",counts);
	
	return 0;
}

5.回溯版8皇后

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

bool hashtable[1000] = {false};
int table[1000];
int counts=0;

void f(int n, int index){
	
	if(index==n+1){
		counts++;
		return;
	}
	
	for(int i=1;i<n+1;i++){
		if(hashtable[i]==false){
			bool flag = true;
			
			for(int pre=1;pre<index;pre++){
				if(abs(pre-index)==abs(table[pre]-i)){
					flag = false;
					break;
				}
			}
			
			if(flag){
				table[index] = i;
				hashtable[i] = true;
				f(n,index+1);
				hashtable[i] = false;
			}
			
		}
	}
}


int main(){
	
	int n;
	scanf("%d",&n);
	f(n,1);
	
	printf("%d\n",counts);
	
	return 0;
}

6.吃糖果

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

int counts=0;

int f(int n){
	if(n==1){
		return 1;
	}else if(n==2){
		return 2;
	}else{
		return f(n-1)+f(n-2);
	}
}


int main(){
	
	int n;
	scanf("%d",&n);
	
	int x=f(n);
	
	
	printf("%d\n",x);
	
	
	return 0;
}

7.数列

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

int f(int n){
	if(n==0){
		return 1;
	}else if(n==1){
		return 1;
	}else{
		return f(n-1)+f(n-2);
	}
}



int main(){
	
	int n;
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
		
		for(int j=0;j<2*(n-1-i);j++){
			printf(" ");
		}
		
		if(i==0){
			printf("0\n");
		}else{
			
			printf("0");
			int j =0;
			for(j=0;j<2*i-1;j++){
				printf(" %d",f(j));
			}
			
			printf(" %d\n",f(j));
		}
	}
	
	return 0;
}

8.神奇的口袋

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

int n;
int counts=0;
int martix[100];
bool hashs[100] = {false};

void f(int lay,int col){
	if(lay==0){
		counts++;
		return;
	}
	
	if(col==n){
		return;
	}
	
	if(lay-martix[col]>=0){
		f(lay-martix[col],col+1);
	}
}

int main(){
	
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
		scanf("%d",&martix[i]);
	}
	
	f(40,0);
	
	printf("%d\n",counts);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aixiaoxiao13/article/details/120691053