递归

1. 计算阶乘

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>

using namespace std;

int f(int n)    // 递归计算阶乘
{
    if (n == 0)
    {
        return 1;
    }
    return f(n-1) * n;
}

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

2. 计算Fibonacci第n项

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>

using namespace std;

int f(int n)    // 递归Fibonacci数列第N项
{
    if (n == 0 || n == 1)
    {
        return 1;
    }
    return f(n-1) + f(n-2);
}

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

3. 输出全排列(Full Permutation)

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>

using namespace std;

const int maxn = 11;
// p[]表示当前排列,hashTable[i]记录i是否存在于当前排列p[]中
int n, p[maxn], hashTable[maxn] = {false};

void generateP(int index)   // index表示排列中当前位置
{
    if (index == n + 1) // 递归边界,处理完n位
    {
        for (int i= 1; i <= n; i++)
        {
            printf("%d ", p[i]);
        }
        printf("\n");
        return;
    }
    for (int i = 1; i <= n; i++)    // 从1~n枚举,填入排列
    {
        if (hashTable[i] == false)  // 排列中没有i
        {
            p[index] = i;   // 将i存入排列
            hashTable[i] = true;    // 改变i的状态
            generateP(index+1);     // 处理下一个位置index+1
            hashTable[i] = false;   // 恢复i的状态
        }
    }
}

int main()
{
    scanf("%d", &n);
    generateP(1);   // 从第一个位置开始处理
    return 0;
}

4. N皇后问题(全排列,暴力)

思路:

n个皇后在n*n的棋盘上既不能同行,也不能同列,还不能在一条对角线上。

  1. 首先,我们保证n个皇后分别在n列上。(保证不在同一列)
  2. 然后,对这n个皇后的行号进行全排列。(保证不在同一行)
  3. 最后,排除掉在同一对角线的情况。(保证不在同一对角线)
    判断在同一对角线的方法如下:

\这样的斜线,同一对角线,那么x1-y1=x2-y2;
/这样的斜线,同一对角线,那么x1+y1=x2+y2;

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>

using namespace std;

const int maxn = 11;

int cnt = 0, n;
// 若p[i] = x,则i代表皇后所在列,x代表皇后所在行
// hashTable[x] = true,代表x已经进入排列p[]中
int p[maxn], hashTable[maxn] = {false};

void generateP(int index)
{
    if (n+1 == index)
    {
        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;
                    break;
                }
            }
        }
        if (flag)
        {
            cnt++;
        }
        return;
    }
    for (int i = 1; i <= n; i++)	// 对行号进行全排列
    {
        if (hashTable[i] == false)
        {
            p[index] = i;
            hashTable[i] = true;
            generateP(index+1);
            hashTable[i] = false;
        }
    }
}

int main()
{
    scanf("%d", &n);
    generateP(1);   // 从第一个位置开始处理
    printf("%d\n", cnt);
    return 0;
}

5. N皇后问题优化(回溯算法)

回溯法:一般来说,如果在到达递归边界前的某层,由于一些事实导致已经不需要往任何一个子问题递归,就可以直接返回上一层。

思路:

在进行第index列,第i行的皇后放置时,将其与之前放置的皇后们进行冲突判定。若冲突,则直接判断下一行。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>

using namespace std;

const int maxn = 11;

int cnt = 0, n;
int p[maxn], hashTable[maxn] = {false};

void generateP(int index)
{
    if (index == n+1)	// 前N行判断完,递归结束
    {
        cnt++;
        return;
    }
    // 对N个行号进行全排列
    for (int i = 1; i <= n; i++)
    {
    	// 若第i行没有放置皇后,则进行处理
        if (hashTable[i] == false)
        {
            bool flag = true;
            for (int j = 1; j < index; j++)	// 枚举前面已经排好的列
            {
            	// 若第index列,i行的皇后与之前的某个皇后冲突,则直接判断下行
                if (abs(index - j) == abs(i - p[j]))	
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                p[index] = i;
                hashTable[i] = true;
                generateP(index+1);
                hashTable[i] = false;
            }
        }
    }
}

int main()
{
    scanf("%d", &n);
    generateP(1);   // 从第一个位置开始处理
    printf("%d\n", cnt);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值