<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
</body>
</html>
<script type="text/javascript">
//公园里有一堆桃子,猴子每天吃掉一半,挑出一个坏的扔掉,第6天的时候发现还剩1个桃子,问原来有多少个桃子
//函数功能 返回第n天的桃子数量
//递归就是函数自己调自己
var sl;
function ShuLiang(n)
{
if(n==6)
{
sl = 1;
}
else
{
//求当天的数量
//当天的数量 = (下一天的数量+1)*2;
sl = (ShuLiang(n+1)+1)*2;
}
return sl;
}
alert(ShuLiang(1)); //输出(ShuLiang(0)) 结果就是190 当天的数量
//输出(ShuLiang(1)) 结果就是94 下一天的数量
/*
ShuLiang(0) ShuLiang(0)就是sl = (94+1)*2 = 190;
{
sl = (ShuLiang(1)+1)*2; 卡主 sl = (94+1)*2
}
ShuLiang(1)
{
sl = (ShuLiang(2)+1)*2; 卡主 sl = (46+1)*2
}
ShuLiang(2)
{
sl = (ShuLiang(3)+1)*2; 卡主 sl = (22+1)*2;
}
ShuLiang(3)
{
sl = (ShuLiang(4)+1)*2; 卡主 sl = (10+1)*2;
}
ShuLiang(4)
{
sl = (ShuLiang(5)+1)*2; 卡主 sl = (4+1)*2;
}
ShuLiang(5)
{
sl = (ShuLiang(5)+1)*2; 卡主 sl = (1+1)*2;
}
ShuLiang(6)
{
return 1; 发现n=6了就返回sl=1 然后往回计算
}
*/
</script>