递归的思想与应用

目录

1、递归的思想

2、递归思想的应用 

1、递归求和

2、斐波拉契数列 

3、求字符串长度 

4、单向链表的转置

5、单向排序链表的合并

6、汉诺塔问题 

7、全排列问题 

3、递归实现回溯算法

1、回溯的本质

2、逆序打印单链表中的偶数结点

3、八皇后问题 

4、小结 


1、递归的思想

递归是—种数学上分而自治的思想 

                -将原问题分解为规模较小的问题进行处理 

                        分解后的问题与原问题的类型完全相同,但规模较小 

                        通过小规模问题的解,能够轻易求得原问题的解 

                -问题的分解是有限的(递归不能无限进行) 

                        当边界条件不满足时,分解问题(递归继续进行) 

                        当边界条件满足时,直接求解(递归结束) 

递归模型的一般表示法 

递归在程序设计中的应用 

                -递归函数 

                         函数体中存在自我调用的函数 

                         递归函数必须有递归出口(边界条件) 

                         函数的无限递归将导致程序崩溃 

 

2、递归思想的应用 

1、递归求和

求解: Sum(n) = 1 + 2 + 3 + … + n 

 

 

#include <iostream>

using namespace std;

unsigned int sum(unsigned int n)
{
    if(n > 1)
        return n + sum(n - 1);
    else
        return 1;
}

int main()
{
    cout << sum(100) << endl;
 
    return 0;
}

2、斐波拉契数列 

               -数列自身递归定义: 1, 1, 2, 3, 5, 8, 13, 21, … 

 

#include <iostream>

using namespace std;

unsigned int fib(unsigned int n)
{
    if(n > 2)
        return fib(n - 1) + fib(n - 2);

    if(n == 1 || n == 2)
        return 1;

    return 0;
}


int main()
{
    for(int i = 1; i < 10; i++)
        cout << fib(i) << endl;

    return 0;
}

3、求字符串长度 

用递归的方法编写函数求字符串长度

                    

 

#include <iostream>

using namespace std;

unsigned int _strlen_(const char* s)
{
//    if(*s != '\0')
//        return 1 + _strlen_(s + 1);
//    else
//        return 0;

    return s ? ( *s ? (1 + _strlen_(s + 1)) : 0 ) : 0;
}

int main()
{
    cout << _strlen_("nyist") << endl;

    return 0;
}

4、单向链表的转置

                             出口:空链表的转置为空链表,单链表只有一个结点不需要转置

#include <iostream>

using namespace std;

struct Node
{
    int value;
    Node* next;
};

Node* create_list(int v, int len)
{
    Node* ret = NULL;
    Node* slider = NULL;

    for(int i = 0; i < len; i++)
    {
        Node* n = new Node();

        n->value = v++;
        n->next = NULL;

        if(slider == NULL)
        {
            slider = n;
            ret = n;
        }
        else
        {
            slider->next = n;
            slider = n;
        }
    }

    return ret;
}

void destory_list(Node* list)
{
    while(list)
    {
        Node* del = list;

        list = list->next;

        delete del;
    }
}

void print_list(Node* list)
{
    while(list)
    {
        cout << list->value << "->";

        list = list->next;
    }

    cout << "NULL" << endl;
}

Node* reserse(Node* list)
{
    if(list == NULL || (list->next == NULL)) //空表或单链表只有一个结点
    {
        return list;
    }
    else
    {
        Node* guard = list->next;//标记第1个结点
        Node* ret = reserse(list->next);//子表的转置

        guard->next = list;//转置后,将标记的结点指向第0个结点

        list->next = NULL;  //原来首结点的next置NULL

        return ret;
    }
}
int main()
{
    Node* list = create_list(1, 5);

    print_list(list);

    list = reserse(list);

    print_list(list);

    destory_list(list);

    return 0;
}

     

5、单向排序链表的合并

两个有序的单链表合并

 

#include <iostream>

using namespace std;

struct Node
{
    int value;
    Node* next;
};

Node* create_list(int v, int len)
{
    Node* ret = NULL;
    Node* slider = NULL;

    for(int i = 0; i < len; i++)
    {
        Node* n = new Node();

        n->value = v++;
        n->next = NULL;

        if(slider == NULL)
        {
            slider = n;
            ret = n;
        }
        else
        {
            slider->next = n;
            slider = n;
        }
    }

    return ret;
}

void destory_list(Node* list)
{
    while(list)
    {
        Node* del = list;

        list = list->next;

        delete del;
    }
}

void print_list(Node* list)
{
    while(list)
    {
        cout << list->value << "->";

        list = list->next;
    }

    cout << "NULL" << endl;
}

Node* merge(Node* list1, Node* list2)
{
    if(list1 == NULL)
    {
        return list2;
    }
    else if(list2 == NULL)
    {
        return list1;
    }
    else if(list1->value < list2->value)
    {
//        Node* list_ = list1->next;
//        Node* list = merge(list_, list2);

//        list1->next = list;

//        return list1;
        return (list1->next = merge(list1->next, list2), list1);
    }
    else
    {
//        Node* list2_ = list2->next;
//        Node* list = merge(list2_, list1);

//        list2->next = list;

//        return list2;

        return (list2->next = merge(list2->next, list1), list2);
    }
}

int main()
{
    Node* list1 = create_list(1, 5);
    Node* list2 = create_list(2, 6);

    print_list(list1);
    print_list(list2);

    Node* list = merge(list1, list2);

    print_list(list);

    destory_list(list);

    return 0;
}

 

6、汉诺塔问题 

汉诺塔问题 

                 -将木块借助B柱由A柱移动到C柱 

                 -每次只能移动—个木块 

                  - 只能出现小木块在大木块之上

                    

汉诺塔问题分解 

               -将n-1个木块借助C柱由A柱移动到B柱 

               -将最底层的唯—木块直接移动到C柱 

               -将n-1个木块借助A柱由B柱移动到C柱 

                

#include <iostream>

using namespace std;

void HanoiTower(int n, char a, char b, char c) //a==>scr  c==>dest  b==>middle
{
    if(n == 1)
    {
        cout << a << "->" << c << endl;
    }
    else
    {
        HanoiTower(n-1, a, c, b);
        HanoiTower(1, a, b, c);
        HanoiTower(n-1, b, a, c);
    }
}

int main()
{
    HanoiTower(3, 'a', 'b', 'c');

    return 0;
}

 

7、全排列问题 

全排列的递归解法 

                

                    分别以a,b,c开始,对后面的部分进行全排列,得到结果

                    abc,bac,cba可以看成abc中a与a交换,a与b交换,a与c交换,

                     即第0个元素分别与后续元素的交换

        

       

#include <iostream>
#include <cstring>

using namespace std;

void permutation(char* s, char* e)
{
    if(*s == '\0')//出口
    {
        cout << e << endl;
    }
    else
    {
        int len = strlen(s);

        for(int i = 0; i < len; i++)
        {
            if(i == 0 || s[0] != s[i])  //避开相同元素情况
            {
                swap(s[0], s[i]);
                permutation(s+1, e);//交换完以后全排列子串
                swap(s[0], s[i]);//交换回去
            }

        }
    }
}

int main()
{
    char s[] = {"abc"};

    permutation(s, s);

    cout << endl;

    char a[] = "aac";

    permutation(a, a);

    return 0;
}

  

#include <iostream>

using namespace std;

void swap(int arr[], int i, int j)
{
    int t = arr[i];
    arr[i] = arr[j];
    arr[j] = t;
}

void printArray(int arr[], int n)
{
    for(int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

void perm(int arr[], int b, int e)
{
    if(b == e)
    {
        printArray(arr, e+1);
    }
    else
    {
        for(int i = b; i <= e; i++)
        {
            if(i == b || arr[i] != arr[b]) // i == b时也要进行全排列
            {
                swap(arr, b, i); // 对每一个元素进行全排列
                perm(arr, b+1, e);
                swap(arr, b, i);
            }
        }
    }
}

int main()
{
    int arr[3] = {1, 2, 3};
    perm(arr, 0, 2);

    return 0;
}

                                            

3、递归实现回溯算法

1、逆序打印单链表中的偶数结点

实例分析:逆序打印单链表中的偶数结点

            

        

#include <iostream>

using namespace std;

struct Node
{
    int value;
    Node* next;
};

Node* create_list(int v, int len)
{
    Node* ret = NULL;
    Node* slider = NULL;

    for(int i = 0; i < len; i++)
    {
        Node* n = new Node();

        n->value = v++;
        n->next = NULL;

        if(slider == NULL)
        {
            slider = n;
            ret = n;
        }
        else
        {
            slider->next = n;
            slider = n;
        }
    }

    return ret;
}

void destory_list(Node* list)
{
    while(list)
    {
        Node* del = list;

        list = list->next;

        delete del;
    }
}

void print_list(Node* list)
{
    while(list)
    {
        cout << list->value << "->";

        list = list->next;
    }

    cout << "NULL" << endl;
}

void r_print_even(Node* list)
{
    if(list != NULL)
    {
        r_print_even(list->next);   //逆序打印后续子表

        if((list->value) % 2 == 0)
        {
            cout << list->value << endl;
        }
    }
}

int main()
{
    Node* list = create_list(2, 5);

    print_list(list);

    r_print_even(list);

    destory_list(list);

    return 0;
}

  

函数调用栈分析

                                        退栈打印的过程其实就是回溯的过程

回溯法也称试探法,它的基本思想是:从问题的某一种状态(初始状态)出发,搜索从这种状态出发所能达到的

所有“状态”,当一条路走到“尽头”的时候(不能再前进),再后退一步或若干步,从另一种可能“状态”出发,继续

搜索,直到所有的“路径”(状态)都试探过。这种不断“前进”、不断“回溯”寻找解的方法,就称作“回溯法”。

 

2、八皇后问题 

八皇后问题 

                       在—个8x8的国际象棋棋盘上,有8个皇后,每个皇后占一格;要求皇后间不会

                       出现相互“攻击”的现象不能有两个皇后处在同—行、同—列或同—对角线上)。 

关键数据结构定义: 

               -棋盘:二维数组(10 * 10) 

                              0表示位置为空,1表示皇后,2表示边界

               -位置:struct Pos; 

struct Pos
{
    int x;
    int y;
};

               -方向: 当前位置数据加上方向数据

                         水平: (-1, 0), (1, 0) 

                         垂直: (0, -1), (0, 1) 

                         对角线: (-1, 1), (-1, -1), (1, -1), (1, 1) 

算法思路 

                1. 初始化: j = 1 

                2. 初始化: i = 1 

                3. 从第j行开始,恢复i的有效值(通过函数调用栈进行回溯 ),判断第i个位置 

                            a. 位置 i 可放入皇后:标记位置( i , j ) , j++, 转步骤2 

                            b. 位置 i 不可放入皇后: i++, 转步骤a 

                            c. 当i > 8时,j--,转步骤3 

               - 结束: 

                            第8行有位置可放入皇后 

编程实验 

                      八皇后问题的递归解法     class QueueSolution; 

#include <iostream>
#include "LinkList.h"

using namespace std;
using namespace DTLib;

template <int SIZE>
class QueueSolution
{
protected:
    enum { N = SIZE + 2 };  // SIZE=8, N=10

    struct Pos : public Object
    {
        Pos(int px = 0, int py = 0) : x(px),y(py) { }
        int x;
        int y;
    };

    int m_chessboard[N][N];
    Pos m_direction[3];//只需要判断三个方向
    LinkList<Pos> m_solution;
    int m_count;

    void init()
    {
        m_count = 0;

        //初始化边界
        for(int i = 0; i < N; i += (N-1))
        {
            for(int j = 0; j < N; j++)
            {
                m_chessboard[i][j] = 2;
                m_chessboard[j][i] = 2;
            }
        }

        for(int i = 1; i <= SIZE; i++)
        {
            for(int j = 1; j <= SIZE; j++)
            {
                m_chessboard[i][j] = 0;
            }
        }

        m_direction[0].x = -1;
        m_direction[0].y = -1;
        m_direction[1].x = 0;
        m_direction[1].y = -1;
        m_direction[2].x = 1;
        m_direction[2].y = -1;
    }

    void print()
    {
        for(m_solution.move(0); !m_solution.end(); m_solution.next())
        {
            cout << "(" << m_solution.current().x << "," << m_solution.current().y << ") ";
        }

        cout << endl;

        for(int i = 0; i < N; i++)
        {
            for(int j = 0; j < N; j++)
            {
                switch(m_chessboard[i][j])
                {
                    case 0: cout << " "; break;
                    case 1: cout << "#"; break;//皇后
                    case 2: cout << "*"; break;//边界
                }
            }

            cout << endl;
        }

        cout << endl;
    }

    //检查三个方向上有没有别的皇后
    bool check(int x, int y, int d)
    {
        bool flag = true;

        do
        {
            x += m_direction[d].x;
            y += m_direction[d].y;
            flag = flag && (m_chessboard[x][y] == 0);
        }
        while( flag );

        return (m_chessboard[x][y] == 2);
    }

    void run(int j)
    {
        if(j <= SIZE)
        {
            for(int i = 1; i <= SIZE; i++)
            {
                if(check(i, j, 0) && check(i, j, 1) && check(i, j, 2))
                {
                    m_chessboard[i][j] = 1; // 放置皇后

                    m_solution.insert(Pos(i, j));

                    run(j + 1);

                    m_chessboard[i][j] = 0;

                    m_solution.remove(m_solution.length() - 1);
                }
            }
        }
        else
        {
            m_count++;

            print();
        }
    }

public:
    QueueSolution()
    {
        init();
    }

    void run()
    {
        run(1);

        cout << "Total :" << m_count << endl;
    }

};

int main()
{
    QueueSolution<8> qs;

    qs.run();

    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值