递归有两个重要概念:递归边界和递归式(调用)
示例1:使用递归求解n的阶乘
#include <cstdio>
using namespace std;
int F(int n) {
if (n == 0) return 1; //递归边界为0
else return F(n - 1)*n; //没有达到递归边界时,使用递归式递归调用下去
}
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", F(n));
return 0;
}
示例2:求Fibonacci数列的第n项
#include "stdafx.h"
#include <cstdio>
using namespace std;
int F(int n) {
if (n == 1 || n == 2) return 1;
else return F(n - 1) + F(n - 2);
}
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", F(n));
return 0;
}
以上这段代码,其实也是分治思想的一种体现问题F(n)是两个子问题F(n-1)和F(n-2)的合并。
示例3:全排列问题(Full Permutation)
#include "stdafx.h"
#include <cstdio>
const int maxn = 11;
int n, P[maxn], hashTable[maxn] = { false };
using namespace std;
void rowF(int index) {
if (index == n + 1) //递归边界
{
for (int i = 1; i <= n; i++)
{
printf("%d", P[i]); //输出当前自问题的解
}
printf("\n");
return;
}
else //没有达到递归边界时,使用递归式递归调用下去
{
for (int i = 1; i <= n; i++)
{
if (hashTable[i] == false)
{
P[index] = i;
hashTable[i] = true;
rowF(index + 1);
hashTable[i] = false;
}
}
}
}
int main()
{
n = 3;
rowF(1); //从第一行开始
return 0;
}
示例4:n皇后问题
n*n的矩阵上n个皇后两两均不在同一行、同一列、同一条对角线上,求合法的方案数。书上提供了一个很好的想法:考虑到每行每列都只能放置一个皇后,把n列皇后的所在行号依次写出,就会是一个1~n的排列,这就下降成一个全排列问题+检验是否在同一对角线的问题。
#include "stdafx.h"
#include <cstdio>
#include<math.h>
const int maxn = 11;
int count = 0;
int n, P[maxn], hashTable[maxn] = { false };
using namespace std;
void rowF(int index) {
if (index == n + 1) //递归边界
{
//for (int i = 1; i <= n; i++) printf("%d", P[i]); //输出当前问题的解
//printf("\n");
bool flag = true; //表示当前排列为一个合法方案
for (int i = 1; i <= n; i++) //遍历任意两个皇后
{
for (int j = i + 1; j <= n; j++)
{
if (abs(i - j) == abs(P[i] - P[j])) //如果在同一条对角线上
{
flag = false; //不合法
}
}
}
if (flag) count++; //若当前方案合法,令count+1 这里是否要担心把列的组合算进去呢? 不用,不存在列的组合吗?不存在吗 不存在
return;
}
else //没有达到递归边界时,使用递归式递归调用下去
{
for (int i = 1; i <= n; i++)
{
if (hashTable[i] == false)
{
P[index] = i;
hashTable[i] = true;
rowF(index + 1);
hashTable[i] = false;
}
} //这里的i是一个列的循环,没有返回,到n即停(当然也可以看成为行服务),所以上面的index==n+1更像是为行服务,一行到头,则输出行/做出合法判断
} //为什么行能返回列不会返回导致无限循环呢,因为有个 hashTable[i] = false;吗?
} //不是,因为对于列而言,其实只有一个for语句,在这个for语句里,进行完就完了
//但是对于行而言,它是列语句每个for语句里的一个小的递归(递归的集合直到行结束,然后会回到列的for语句中)
int main()
{
scanf("%d", &n);
rowF(1); //从第一行开始
printf("%d\n", count);
return 0;
}
注意这里面的那段注释,帮助理解,其实现在对于全排列的理解还是不够透彻
对以上的代码思路进行 改进,前面的思路实质上还是暴力穷举法
可以发现,对于一些情况,当已经放置了一部分的皇后时(生成了排列的一部分),可能剩余的皇后无论怎样放置都不合法,就不用往下递归了,直接返回上层
回溯法定义: 如果在到达递归边界前的某层,由于一些事实导致已经不需要往任何一个子问题递归,就可以直接返回上一层
以下是改进的代码:
#include "stdafx.h"
#include <cstdio>
#include<math.h>
const int maxn = 11;
int count = 0;
int n, P[maxn], hashTable[maxn] = { false };
using namespace std;
void rowF(int index) {
if (index == n + 1) //递归边界
{
count++; //若当前方案合法,令count+1
return;
}
else //没有达到递归边界时,使用递归式递归调用下去
{
for (int i = 1; i <= n; i++) //第i行
{
if (hashTable[i] == false) //第i行还没有皇后
{
bool flag = true;
for (int pre = 1; pre < index; pre++)
//第index列皇后的行号为i,第pre列皇后的行号为P[pre]
{
if (abs(index - pre) == abs(i - P[pre]))
{
flag = false; //同一对角线,冲突
break;
}
}
if (flag)
{
P[index] = i;
hashTable[i] = true; //第x行被占用
rowF(index + 1); //递归处理第index+1行皇后
hashTable[i] = false; //递归完毕,换原第x行未占用
}
}
}
}
}
int main()
{
scanf("%d", &n);
rowF(1); //从第一行开始
printf("%d\n", count);
return 0;
}
练习
吃糖果
题目描述
名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N >0)。
妈妈告诉名名每天可以吃一块或者两块巧克力。
假设名名每天都吃巧克力,问名名共有多少种不同的吃完巧克力的方案。
例如:
如果N=1,则名名第1天就吃掉它,共有1种方案;
如果N=2,则名名可以第1天吃1块,第2天吃1块,也可以第1天吃2块,共有2种方案;
如果N=3,则名名第1天可以吃1块,剩2块,也可以第1天吃2块剩1块,所以名名共有2+1=3种方案;
如果N=4,则名名可以第1天吃1块,剩3块,也可以第1天吃2块,剩2块,共有3+2=5种方案。
现在给定N,请你写程序求出名名吃巧克力的方案数目。
输入
输入只有1行,即整数N。
输出
可能有多组测试数据,对于每组数据,
输出只有1行,即名名吃巧克力的方案数。
样例输入
1
2
4
样例输出
1
2
5
#include <cstdio>
#include<math.h>
int count;
void func(int index)
{
if (index == 0) return;
if (index==1)
{
count++;
return;
}
if (index == 2)
{
count += 2;
}
if (index != 0 && index != 1 && index != 2 )
{
for (int i = 1; i <= 2; i++)//这里有没有等号?我觉得有:今天吃i块
{
func(index-i);
}
}
}
int main()
{
int N;
while (scanf("%d", &N) != EOF)
{
count = 0;
func(N);
printf("%d\n", count);
}
return 0;
}
数列
题目描述
编写一个求斐波那契数列的递归函数,输入n 值,使用该递归函数,输出如下图形(参见样例)。
输入
输入第一行为样例数m,接下来有m行每行一个整数n,n不超过10。
输出
对应每个样例输出要求的图形(参见样例格式)。
#include <cstdio>
#include<math.h>
int outputraw[30];
int func(int index)
{
if (index == 1)
{
return 0;
}
if (index == 2)
{
return 1;
}
if (index != 2 && index != 1)
{
return func(index - 1) + func(index - 2);
}
}
int main()
{
int N,M;
int temp = 1;
while (scanf("%d", &M) != EOF)
{
for (int j = 0; j < M; j++)
{
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
for (int j = temp; j <= 2 * i + 1; j++)
{
outputraw[j] = func(j);
temp = j;
}
for (int k = 1; k < N - i; k++)
{
printf(" ");
}
for (int j = 1; j <= 2 * i + 1; j++)
{
printf("%d", outputraw[j]);
if (j != 2 * i + 1)
printf(" ");
}
printf("\n");
}
}
}
return 0;
}
神奇的口袋
这道题还不会做!
题目描述
有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在的问题是,John有多少种不同的选择物品的方式。
输入
输入的第一行是正整数n (1 <= n <= 20),表示不同的物品的数目。接下来的n行,每行有一个1到40之间的正整数,分别给出a1,a2……an的值。
输出
输出不同的选择物品的方式的数目。
八皇后
题目描述
会下国际象棋的人都很清楚:皇后可以在横、竖、斜线上不限步数地吃掉其他棋子。如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题。
对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2…b8,其中bi为相应摆法中第i行皇后所处的列数。已经知道8皇后问题一共有92组解(即92个不同的皇后串)。
给出一个数b,要求输出第b个串。串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小。
输入
第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数b(1 <= b <= 92)
输出
输出有n行,每行输出对应一个输入。输出应是一个正整数,是对应于b的皇后串。
#include <string>
#include <iostream>
#include "stdafx.h"
#include<math.h>
#include <cstdio>
int output[100] = { 0 };
int temp[10];
bool hashTable[10] = { false };
int c_count = 1;
int x = 8;
void func_g(int index)
{
if (index == x+1)
{
for (int j = 1; j <= x; j++)
{
output[c_count] = output[c_count] * 10 + temp[j];
}
c_count++;
return;
}
else
{
for (int i = 1; i <= x; i++)
{
if (hashTable[i] == false)
{
bool flag = true;
for (int p = 1; p < index; p++)
{
if (abs(p - index) == abs(i - temp[p]))
{
flag = false;
break;
}
}
if (flag)
{
temp[index] = i;
hashTable[i] = true;
func_g(index + 1);
hashTable[i] = false;
}
}
}
}
}
int main()
{
int n;
int b[200] = { 0 };
func_g(1);
sort(output+1, output + c_count);
while (scanf("%d", &n) != EOF)
{
for (int i = 1; i <= n; i++)
{
scanf("%d", &b[i]);
printf("%d\n", output[b[i]]);
}
}
}