一堆苹果一天吃一般再多一个,第二又吃掉一半零一个,第十天后发现就剩下一个了,求一共有多少个苹果,用JavaScript代码怎么写?
JavaScript问题,哪位朋友帮帮忙?
- 提问者网友:萌卜娃娃
- 2021-05-11 07:09
- 五星知识达人网友:十年萤火照君眠
- 2021-05-11 08:34
<!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>
<div id="num"></div>
<script type="text/javascript">
var peach=1;
var num=document.getElementById("num");
num.innerHTML+="第10天的桃子数量是:1<br/>";
for(var i=9;i>0;i--)
{
peach=(peach+1)*2;
num.innerHTML+="第"+i+"天的桃子数量是:"+peach+"<br/>";
}
num.innerHTML+="桃子总数是:"+peach+"<br/>";
</script>
</body>
</html>
- 1楼网友:慢性怪人
- 2021-05-11 08:48
代码如下:
<script language="javascript">
x=1
for(i=1;i<=10;i++)
{
x=x*2+1
} document.write(x+"<br>"); for(i=1;i<=10;i++) { document.write("第"+i+"天吃了"+(parseInt(x/2)+1)+"个,剩于"+(x-(parseInt(x/2)+1))+"<br>"); x=x-(parseInt(x/2)+1); } </script>