for循环经典案例

1.棋盘放粮食
棋盘有32个格子,第一个格子放1个芝麻,第二个放2个,第三个放4个,第四个放8个。。。每个芝麻的重量为0.00001kg,如果要放满整个棋盘,需要多少重量的芝麻。

<!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>
    <script type="text/javascript">
              var a=0;//芝麻的个数
          for(i=0;i<32;i++)
          {
        var sl=1;
        for(k=0;k<i;k++)
        {
           sl=sl*2;
           m=sl*0.0001;//每次的重量
         }
        a=a+sl;
        k=a*0.0001;
        alert(k);    
 </script>
    
    
</html>

2.折纸:折多少次和珠穆朗玛峰一样高
1.一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米

<!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>
    <script type="text/javascript">
                    var hou=0.0001;
            for(i=0;i>=0;i++)
            {
            hou=hou*2;
            if(hou>8848)
            {
                break;    
            }
            }
             alert(i+1);
</script>
    
    
</html>

3.羽毛球拍15元,球3元,水2元。200元每种至少一个,有多少可能

羽毛球拍最多买13个 球最多买66个 水最多买100个

<!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>
    <script type="text/javascript">
var cishu=0;
            for(y=1;y<14;y++)
            {
                for(q=1;q<67;q++)
                {
                    for(s=1;s<101;s++)
                    {
                        if(y*15+q*3+s*2==200)
                        {
                            cishu=cishu+1;
                        }
                    }
                }
            }
                            alert(cishu);
</script>
    
    
</html>

4.公鸡2文,母鸡1文,小鸡半文,每种至少一只,100文买100只鸡有多少可能性?

<!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>
    <script type="text/javascript">
var sl=0;
                for(g=1;g<51;g++)
                {
                    for(m=1;m<101;m++)
                    {
                        for(x=1;x<201;x++)
                        {
                            if(g*2+m+x*0.5==100 && g+m+x==100)
                            {
                                sl=sl+1;
                            }
                        }
                    }
                }
                                alert(sl);
 </script>
    
    
</html>

5.凑钱1,2,5凑20元钱有多少可能?

<!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>
    <script type="text/javascript">
var sl=0;
                for(a=0;a<21;a++)
                {
                    for(b=0;b<11;b++)
                    {
                        for(c=0;c<5;c++)
                        {
                            if(a+b*2+c*5==20)
                            {
                                sl=sl+1;
                            }
                        }
                    }
                }
                                alert(sl);
 </script>
    
    
</html>

6.括号里面只能放加或减,如果要使等式成立,括号里面应该放什么运算符
12()34()56()78()9 = 59

<!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>
    <script type="text/javascript">
var sl=0;
                for(a=-1;a<2;a=a+2)
                {
                    for(b=-1;b<2;b=b+2)
                    {
                        for(c=-1;c<2;c=c+2)
                        {
                            for(d=-1;d<2;d=d+2)
                            {
                                if(12+a*34+b*56+c*78+d*9==59)
                                {
                                    sl=sl+1;
                                }
                            }
                        }
                    }
                }
                                    alert(sl);
</script>
    
    
</html>
原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6039872.html