标签:国际象棋 cout 2.3 实例 new bubuko 执行 实现 问题:
递归是一种数学上分而自治的思想:
递归模型的一般表示法:
递归在程序设计中的应用:
递归思想的应用:
递归求和:
unsigned int sum(unsigned int n)
{
if( n > 1 )
{
return n + sum(n-1);
}
else
{
return 1;
}
}
斐波拉契数列:
数列自身递归定义:1, 1, 2, 3, 5, 8, 13, 21, ...
斐波拉契数列:
unsigned int fac(unsigned int n)
{
if( n > 2 )
{
return fac(n-1) + fac(n-2);
}
if( (n == 2) || (n == 1) )
{
return 1;
}
return 0;
}
用递归的方法编写函数求字符串长度:
用递归的方法编写函数求字符串长度:
unsigned int _strlen_(const char* s)
{
if( *s != ‘\0‘ )
{
return 1 + _strlen_(s+1);
}
else
{
return 0;
}
}
或
unsigned int _strlen_(const char* s)
{
return s ? (*s ? (1 + _strlen_(s+1)) : 0) : 0;
}
预备的单链表:
#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 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;
}
int main()
{
Node* list = create_list(1, 5);
print_list(list);
destroy_list(list);
return 0;
}
运行结果为:
1->2->3->4->5->NULL
单向链表的转置:
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;
}
运行结果为:
1->2->3->4->5->NULL
5->4->3->2->1->NULL
单向排序链表的合并:
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_1 = list1->next;
Node* list = merge(list_1, list2);
list1->next = list;
return list1;
}
else
{
Node* list_2 = list2->next;
Node* list = merge(list1, list_2);
list2->next = list;
return 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->NULL
2->3->4->5->6->7->NULL
1->2->2->3->3->4->4->5->5->6->7->NULL
代码优化:
Node* merge(Node* list1, Node* list2)
{
if( list1 == NULL )
{
return list2;
}
else if( list2 == NULL )
{
return list1;
}
else if( list1->value < list2->value )
{
return (list1->next = merge(list1->next, list2), list1);
}
else
{
return (list2->next = merge(list1, list2->next), list2);
}
}
汉诺塔问题:
汉诺塔问题分解:
汉诺塔问题:
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;
}
运行结果为:
a-->c
a-->b
c-->b
a-->c
b-->a
b-->c
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++)
{
swap(s[0], s[i]);
permutation(s+1, e);
swap(s[0], s[i]);
}
}
}
int main()
{
char s[] = "abc";
permutation(s, s);
return 0;
}
运行结果为:
abc
acb
bac
bca
cba
cab
但是如果存在相同的元素,则会有重复结果,例如:
int main()
{
char s[] = "aac";
permutation(s, s);
return 0;
}
运行结果为:
aac
aca
aac
aca
caa
caa
代码优化:
void permutation(char* s, char* e) // e始终指向字符数组的首元素
{
if( *s == ‘\0‘ )
{
cout << e << endl;
}
else
{
int len = strlen(s);
char mark[256] = {0};
for(int i=0; i<len; i++)
{
if( !mark[s[i]] )
{
swap(s[0], s[i]);
permutation(s+1, e);
swap(s[0], s[i]);
mark[s[i]] = 1;
}
}
}
}
int main()
{
char s[] = "aac";
permutation(s, s);
return 0;
}
运行结果为:
aac
aca
caa
递归还能用于需要回溯穷举的场合。。。
函数调用过程回顾:
程序中的栈区:一段特殊的专用内存区
实例分析:逆序打印单链表中的偶数结点
逆序打印单链表中的偶数结点:
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);
destroy_list(list);
return 0;
}
运行结果为:
2->3->4->5->6->NULL
6
4
2
八皇后问题:
关键数据结构定义:
算法思路:
八皇后问题:
#include <iostream>
#include "LinkList.h"
using namespace std;
using namespace StLib;
template <int SIZE>
class QueueSolution : public Object
{
protected:
enum { N = SIZE + 2 };
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) // 检查第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<4> qs;
qs.run();
return 0;
}
测试四皇后问题的运行结果为:
(2, 1) (4, 2) (1, 3) (3, 4)
******
* # *
*# *
* #*
* # *
******
(3, 1) (1, 2) (4, 3) (2, 4)
******
* # *
* #*
*# *
* # *
******
Total: 2
(八皇后问题一共有92个解。)
标签:国际象棋 cout 2.3 实例 new bubuko 执行 实现 问题:
原文地址:https://www.cnblogs.com/PyLearn/p/10146924.html