发段忽悠人的二分法查找代码
#include <iostream>using namespace std;
int test[]={1,2,3,4,5,6,7,8,9,10};
bool Find(int nFirst,int nEnd,int nFind)
{
if(nFirst == nEnd) return test==nFind;
int nLeft=(nFirst+nEnd)/2;
return (test>=nFind && Find(nFirst,nLeft,nFind)) ||
(test<=nFind && Find(nLeft+1,nEnd,nFind));
}
int main(int argc, char* argv[])
{
cout<<"膜拜下小齐~@!"<<endl;
Find(0,9,9)==1?cout<<"找到了"<<endl:cout<<"没找到"<<endl;
return 0;
} /:L .......又BS我!...... 写的好简单阿,一看到递归就头晕/:L 很强大啊 其实把代码写成这样根本就没有意义 就跟那个汉诺塔一样
#include <iostream>
using namespace std;
void mov(int n,char a,char b,char c)
{
if(n==0)return;
mov(n-1,a,c,b);
cout<<a<<"-->"<<c<<endl;
mov(n-1,b,a,c);
}
int main(int argc, char* argv[])
{
mov(3,'a','b','c');
return 0;
}
告诉别人汉诺塔这样写 其实没啥意义
bool Find(int nFirst,int nEnd,int nFind)
{
if(nFirst == nEnd) return test==nFind?1:0;
if(test==nFind || test==nFind) return true; // 这里属于优化代码 省略
int nLeft=(nFirst+nEnd)/2;
if(test>=nFind) // 将一下两组if指令用老师教的&&来等价替换
if(Find(nFirst,nLeft,nFind))
return true;
if(test<=nFind)
if(Find(nLeft+1,nEnd,nFind))
return true;
return false;
}
重要是递归的思想、本质 和 递归的建模你是否搞明白了 这才是核心 嗯嗯,老秦命名包准了好多/:023 /:QQ2 人家当初是支初国语呀! 向小齐命名法看齐 ~~ 别,我的命名很烂的,编程风格超超的非常之不错!
页:
[1]