Recursion method application

Recursion method application

Company income issues

2009 revenue of 10 billion yuan, assuming an annual growth rate of 25%, in which year will the company's revenue be 50 billion yuan

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			//2009年收入100亿元,假定每年增长25%,到哪一年公司收入500亿
			var year=2009;
			var sums=100;
			while (sums<500){
     
     //当营业额小于500进入循环
				sums*=1.25;//25%增长      简化前的公式sums=suns*1.25
				year++;//年份+1
			}
			document.write(year+"年收入为"+sums+"超过500亿元");
		</script>
	</body>
</html>

Monkey eating peach problem

Task: The monkey eats peaches. Problem: The monkey picked off a few peaches on the first day, and ate half of it immediately, but was not addicted, and ate one more. The next morning, he ate half of the remaining peaches and ate one more peach. . After that, every morning I ate half and one of the remaining half of the previous day. When I wanted to eat again in the morning of the 7th day, I saw that there was only one peach left. How many were picked on the first day?

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<!--(1)、任务:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,
			又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上
		都吃了前一天剩下 的一半零一个。到第7天早上想再吃时,见只剩下一个桃子了。
		求第一天共摘了多少?-->
		<script type="text/javascript">
			//第七天1个
			//第六天x6=(1+1)*2
			//第五天x5=(x6+1)*2
			//S(n-1)=2*(Sn+1)
			var x = 1; //第七天
			for(var i = 1; i < 7; i++) {
     
     
				x = (x + 1) * 2; //第六天
			}
			document.write(x);
		</script>
	</body>

</html>

Ball in free fall

Fall freely from a height of 100 meters, bounce back to half of the original height after each fall, and then fall again, find the height of the 10th rebound

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<!--从100米高度自由落下,每次落体后反跳回原来高度的一半,再落下,求它第10次反弹的高度-->
		<script type="text/javascript">
		                                     //h=100
			//100/2第一次落下又弹起的高度              h=h/2
			//100/2^2第二次落下又弹起的高度          h=h/2
			//100/2^3第三次落下又弹起的高度
			//100/2^10第十次落下又弹起的高度
			var h=100;//初始值
			for (var i=1;i<=10;i++) {
     
     
				h=h/2
				if (i==10) {
     
     
					document.write("第10次反弹的高度:"+h);
				}
			}
		</script>
	</body>
</html>

Sequence

The law is 1, 1, 2, 3, 5, 8, 13, 21 and output the first fifteen numbers

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<!--规律为1,1,2,3,5,8,13,21输出前十五个数-->
		<!--第一个数=1-->
		<!--第二个数=第一个+0-->
		<!--第三个数=第二个+第一个-->
		<!--第四个数=第三个+第二个-->
		<script type="text/javascript">
			var m = 1; //初始值
			var x = 0;
			for(var i = 0; i < 8; i++) {
     
     
				m = m + x;
				x = m + x;
				document.write(m + "<br />" + x + "<br />");

			}
		</script>
	</body>

</html>

Guess you like

Origin blog.csdn.net/chaotiantian/article/details/114630999