44 递归的思想与应用(中)

原文:https://www.cnblogs.com/wanmeishenghuo/p/9678143.html参考狄泰软件相关教程

 

 

将大问题分解,先将第一个节点拿出来,将其它的节点看成一个整体。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cstring>
#include "DTString.h"
 
using namespace std;
using namespace DTLib;
 
struct Node
{
    int value;
    Node* next;
};
 
Node* create_list(int v, int len)  // v:数据元素从哪一个之开始。 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 destroy_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* reverse(Node* list)
{
    if( (list == NULL) || (list->next == NULL) )
    {
        return list;
    }
    else
    {
        Node* guard = list->next;
        Node* ret = reverse(list->next);
 
        guard->next = list;
 
        list->next = NULL;
 
        return ret;
    }
}
 
int main()
{
    Node* list = create_list(1, 5);
 
    print_list(list);
 
    list = reverse(list);
 
    print_list(list);
 
    destroy_list(list);
 
    return 0;
}

 

 

实验2:

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <cstring>
#include "DTString.h"
 
using namespace std;
using namespace DTLib;
 
struct Node
{
    int value;
    Node* next;
};
 
Node* create_list(int v, int len)  // v:数据元素从哪一个之开始。 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 destroy_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* reverse(Node* list)
{
    if( (list == NULL) || (list->next == NULL) )
    {
        return list;
    }
    else
    {
        Node* guard = list->next;
        Node* ret = reverse(list->next);
 
        guard->next = list;
 
        list->next = NULL;
 
        return ret;
    }
}
 
Node* merge(Node* list1, Node* list2)
{
    if( list1 == NULL )
    {
        return list2;
    }
    else if( list2 == NULL )
    {
        return list1;
    }
    else if( list1->value < list2->value )
    {
        /*
        Node* list1_ = list1->next;
        Node* list = merge(list1_, list2);
 
        list1->next = list;
        return list1;
        */
        return (list1->next = merge(list1->next, list2), list1); //逗号表达式
    }
    else
    {
        /*
        Node* list2_ = list2->next;
        Node* list = merge(list1, list2_);
 
        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);
 
    destroy_list(list);
 
    return 0;
}

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstring>
#include "DTString.h"
 
using namespace std;
using namespace DTLib;
 
 
void HanoiTower(int n, char a, char b, char c) // a ==> src  b ==> middle  c ==> dest
{
    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;
}

 

 

 

 

 

 e始终指向字符串的开头,用来打印,s用来控制交换。

一开始,a和a交换,全排列b、c。

然后,a和b交换,全排列a、c。

然后交换a和c,全排列b、a。

如果两个字符是一样的,就没有必要交换,否则全排列有重复的现象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstring>
#include "DTString.h"
 
using namespace std;
using namespace DTLib;
 
 
void HanoiTower(int n, char a, char b, char c) // a ==> src  b ==> middle  c ==> dest
{
    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);
    }
}
 
void permutation(char* s, char* e)  // 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";
    char s1[] = "aac";
 
    permutation(s, s);
    cout << "----------" << endl;
    permutation(s1, s1);
 
    return 0;
}

 

 

  

  

  

 

posted on   lh03061238  阅读(133)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示