用C 和PHP实现递归算法实例-数据结构与算法学习
递归算法:就是一种直接或间接调用自身的算法。
实现过程:通过函数或者子过程来完成,在函数或者子过程中编写代码直接或间接的调用自己,即可完成递归操作。(相同类别的问题,把问题层层转换为规模缩小的子问题到最小问题有已知条件,然后来求解,然后得到结果逐级返回。其实也是一种循环。)
最主要体现:小的代码量解决了非常复杂的问题
特点:
1、递归就是方法里调用自身
2、必须有一个明确的递归结束条件,称为递归出口。
3、简洁但是运行效率较低,一般不提倡使用
4、每一层的返回点、局部变量等开辟了栈来存储,递归次数过多容易造成栈溢出。
实例1:求阶乘
C 代码:
<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy2162')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy2162" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">#include<iostream>
int factorial(int n);
int main()
{
using namespace std;
int n;
cout << "请输入一个数字:";
cin >> n;
cout << n << "的阶乘为: " << factorial(n) <<endl;
return 0;
}
int factorial(int n)
{
if (n == 1)
return 1;
return n*factorial(n-1);
}</td> </tr> </tbody> </table>
实例2:数制转换
代码:
<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy3889')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy3889" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">#include<iostream>
#include<cstring>
void feelTheBase(char *s, int n, int sys);
int main()
{
using namespace std;
char s[60];
int n,sys;
cout << "请输入一个整数:";
cin >> n;
cout << "请输入要转换的进制类型(2,8,16):";
cin >> sys;
feelTheBase(s, n, sys);
cout << n << "转换成" << sys << "进制结果为: " << s <<endl;
return 0;
}
void feelTheBase(char *s, int n, int sys)
{
char bit[] = {"0123456789ABCDEF"};
int len;
if (n == 0)
{
strcpy(s, "");
return;
}
feelTheBase(s, n/sys, sys);
len = strlen(s);
s[len] = bit[n%sys];
s[len 1] = '';
}</td> </tr> </tbody> </table>
实例3:列出某个目录下所有的子目录和文件(还可以用scandir函数更方便)
PHP实现代码:
<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy1975')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy1975" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2"><?php
function rec($dir, $lev=0){
$dh = opendir($dir);
while (($file = readdir($dh)) != false) {
if ($file == '.' || $file == '..') {
continue;
}
if (is_dir($dir.'/'.$file)){
$arr = explode("/",$dir.'/'.$file);
$lev = count($arr)-3;
echo str_pad('',$lev, "--")."目录".$file."<br/>";
rec($dir.'/'.$file, $lev 1);
}else {
echo str_pad('',$lev, "--").$file."<br/>";
}
}
closedir($dh);
}
$dir = "./";
rec($dir);
?></td> </tr> </tbody> </table>
运行结果:
您可能感兴趣的文章:
PHP递归算法实例解析
一文了解Python中的递归
用C 和PHP实现递归算法实例-数据结构与算法学习
php递归函数小例子
PHP递归算法与应用实例
php递归创建目录小例子
php递归示例 php递归函数代码
python数据分析用什么软件
php递归算法 php递归函数无限级分类
php全排列的递归算法的代码