js计算--一递归
来源:互联网 发布:网络直播商业模式分析 编辑:程序博客网 时间:2024/05/16 06:31
递归:在运行的过程中调用自己。
1、阶乘的递归。
<script>
alert(multiply(5));
function multiply(n){
if(index==1){
return 1;
}
else{
return multiply(n-1)*n;
}}
</script>
递归算法关键:1、递归出口;2、逐渐向出口推进。
阶乘递归理解:出口是为1时就跳出来。else,n-1,逐渐减到1退出。
multiply(5):
当n=5;执行5*multiply(4);
n=4: 执行4*multiply(3)
依次到n=2时:2*multiply(1)
这时候,return 1;往上返回。
2*1向上返回,3*(2*1)向上返回,----5*(4*(3*(2*1)))=120;
2、累加的递归。
function sum(n){
if(n==1){
return n;
}else{
return sum(n-1)+n;
}}
3、斐波那契数或者称谓斐波那契数列。
比如:1、1、2、3、5、8,13,21................第三数开始,等于前两个数相加。
第4个数就是(n-1)+(n-2)=n。
求第n个数是什么?
function Fibonacci(num){
if(num==1||num==2){
return 1;
}else{
return Fibonacci(num-1)+Fibonacci(num-2);
}}
要输出前50个斐波那契数:
var str = new String();
var total = 50;
for(var i= 1;i<total;i++){
str +=Fibonacci(i);
}
4、求一个数的二进制。
十进制转二进制:不断的以除以2得到余数。
当一个数字为1或者0的时候输出本身。
<script>var res = new String();//当字符输出function judge(){var num2 = pardeInt(num/2);<span style="white-space:pre"></span>//通过num2来控制每一次的输出的余数。if(num==1||num==2){res = String(num);}else{judge(num2);res +=String(num%2);}}function output(){
var num = documentElementById('num').value;
judge(num);
alert(res);
}</script>
5、十六进制的递归。
十进制转十六进制:不断的除以16的余数,得到的余数,大于10-15的位A-F
<script>var res = new String();function judge(num){var num2 = parseInt(num/16);if(num2==0){res = String(change(num%16));<span style="white-space:pre"></span>//将传进来的数字要转换}else{judge(num2);res +=String(change(num%16));}}
<pre style="background-color: rgb(255, 255, 255); font-family: 宋体; font-size: 14.3pt;"><span style="color:#000080;"><strong>function </strong></span><span style="font-style:italic;">change</span>(n){ <span style="color:#000080;"><strong>switch </strong></span>(n){ <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">0</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">0</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">1</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">1</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">2</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">2</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">3</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">3</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">4</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">4</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">5</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">5</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">6</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">6</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">7</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">7</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">8</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">8</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">9</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#0000ff;">9</span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">10</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#008000;"><strong>'A'</strong></span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">11</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#008000;"><strong>'B'</strong></span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">12</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#008000;"><strong>'C'</strong></span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">13</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#008000;"><strong>'D'</strong></span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">14</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#008000;"><strong>'E'</strong></span>;<span style="color:#000080;"><strong>break</strong></span>; <span style="color:#000080;"><strong>case </strong></span><span style="color:#0000ff;">15</span>: <span style="color:#000080;"><strong>return </strong></span><span style="color:#008000;"><strong>'F'</strong></span>;<span style="color:#000080;"><strong>break</strong></span>; }}
function output(){ var num=document.getElementById('num').value; judge(num); alert(res);}</script>
- js计算--一递归
- 一:js高级编程之:递归
- 一到递归??的js题
- 递归计算
- js 计算 Fibonacci 值,用到知识点:递归调用"匿名函数"
- js 递归
- js递归
- js递归
- js 递归
- js 递归
- js 递归
- js 递归
- JS递归
- ;递归求和;递归计算阶乘
- 递归(一)
- 一.递归
- 递归一
- [JS] JS递归常见问题
- 浏览器最小字号
- Centos网络配置
- VMware Workstation12Pro 安装linux教程
- web.xml配置文件
- Dirichlet Distribution(狄利克雷分布)与Dirichlet Process(狄利克雷过程)
- js计算--一递归
- hibernate的三种状态
- LOJ 1307 - Counting Triangles(二分)
- NOIP2015 信息传递 解题报告(强连通分量)
- uc-osII实时操作1
- Zookeeper 实现负载均衡
- 剑指offer-反转链表
- 通过Ajax技术,将数组类型数据转换为JSON格式的对象
- found that when click menu then click again, back to main activity but not the one when click menu.