0724循环嵌套和while语句

一.for的循环嵌套

 //for循环的嵌套
            //打印矩阵
            //for (int k = 1; k <= 5;k++ )
            //{
            //    for (int i = 1; i <= 5;i++ )
            //    {
            //        Console.Write("■");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadLine();

            //请输入一个正整数
            //根据这个数打印这个数的行列的在右下角的三角形
            //Console.Write("请输入一个正整数:");
            //int b = int.Parse(Console.ReadLine());
            //for (int i = 1; i <= b; i++)
            //{
            //    for (int k = 1; k <= b - i; k++)
            //    {
            //        Console.Write("  ");
            //    }
            //    for (int a = 1; a <= i; a++)
            //    {
            //        Console.Write("◆");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadLine();

            //输入一个整数,求1!+2!+3!+...n!
            //Console.Write("请输入一个正整数:");
            //int a = int.Parse(Console.ReadLine());
            //int b = 1; int c = 0;
            //for (int i = 1; i <= a; i++)
            //{
            //    b *= i;
            //    for (int d = i; d <= i; d++)
            //    {
            //        c += b;
            //    }
            //}
            //Console.WriteLine(c);
            //Console.ReadLine();


            //打印9*9乘法表
            //int b = 1;
            //for (int a = 1; a <= 9; a++)//控制行数
            //{
            //    for (int d = 1; d <= a; d++)
            //    {
            //        b = a * d;
            //        Console.Write(d+"*"+a+"="+b+" ");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadLine();

            //循环能够解决两类问题:穷举 迭代
           
            //for穷举
            //把所有可能性都列出来
            //单位给150元购物卡
            //洗发水15  香皂2 牙刷5
            //刚好花完150,有几种买法,都买了什么。
            //int ci = 0;
            //int biao = 0;
            //for (int x = 0; x <= 10; x++)
            //{
            //    for (int y = 0; y <= 30; y++)
            //    {
            //        for (int z = 0; z <= 75; z++)
            //        {
            //            ci++;
            //            if (15 * x + y * 5 + z * 2 == 150)
            //            {
            //                biao++;
            //                Console.WriteLine("第{0}种买法:洗发水{1}瓶,牙刷{2}支,香皂{3}块。", biao, x, y, z);
            //            }
            //        }
            //    }
            //}
            //Console.WriteLine("总共有{0}种买法。", biao);
            //Console.WriteLine("总共计算了"+ci+"次。");
            //Console.ReadLine();


            //迭代,从初始情况按照规律不断求解中间情况,最终推导出结果。
            //纸张可以无限次对折,纸张厚度为0.07毫米。
            //问多少次对折至少可以超过8848?
            //double height = 0.07;
            //int ci = 0;
            //while (height <= 8848000)
            //{
            //    ci++;
            //    height *= 2;
            //}
            //Console.WriteLine("经过"+ci+"次折叠能够超过8848米。");
            //Console.ReadLine();

            //五个小朋友站成排
            //后面每个都说比前面那个小两岁
            //最后一个3岁
            //求第一个岁数
            //int a = 3;
            //int b = 0;
            //for (; ; )
            //{
            //    a += 2;
            //    b++;
            //    if (b == 4)
            //    {
            //        break;
            //    }
            //}
            //Console.WriteLine("这个小朋友{0}岁了。", a);
            //Console.ReadLine();


            //大马驮2石粮食,中等1石粮食,两头小的1石粮食
            //要用100匹马,驼100石粮食,该如何分配
            //int sum = 0;
            //for (int d = 0; d <= 50; d++)
            //{
            //    for (int z = 0; z <= 100; z++)
            //    {
            //        for (int x = 0; x <= 200; x++)
            //        {
            //            if (2 * d + z + 0.5 * x == 100 && d + z + x == 100)
            //            {
            //                Console.WriteLine("使用{0}匹大马,{1}匹中马,{2}匹小马,可以驼完。", d, z, x);
            //                sum++;
            //            }

            //        }

            //    }

            //}
            //Console.WriteLine("总共有{0}种办法。", sum);
            //Console.ReadLine();

            //有1分钱,2分钱,5分钱
            //组合出1.5元钱
            //int sum = 0;
            //for (int d = 0; d <= 150; d++)
            //{
            //    for (int z = 0; z <= 75; z++)
            //    {
            //        for (int x = 0; x <= 30; x++)
            //        {
            //            if (d + 2 * z + 5 * x == 150)

            //            {
            //                Console.WriteLine("组合方式为{0}个一分钱,{1}个两分钱,{2}个五分钱。", d, z, x);
            //                sum++;
            //            }

            //        }

            //    }

            //}
            //Console.WriteLine("总共有{0}种组合方式。", sum);
            //Console.ReadLine();

二.while循环

 //while   当循环

//while循环是for的变形如下
            //int i = 0;
            //for (; i < 5; )//就是while(i<5 )
            //{
            //    Console.WriteLine("你好");
            //    i++;
            //}
            //Console.ReadLine();

            //int i=0;
            //while(i<5)
            //{
            //    Console.WriteLine("你好");
            //    i++;
            //}
            //Console.ReadLine();

            //do  while
            //int i = 0;
            //do //不管判断是否正确,先执行
            //{
            //    Console.WriteLine("你好");
            //}
            //while (i > 5);
            //Console.ReadLine();

            //折纸0.07mm 对折多少次超过8848m
            //int ci = 0;
            //double height = 0.07;
            //while (true)
            //{
            //    height *= 2;
            //    ci++;
            //    if (height >= 8848)
            //    {
            //        break;

            //    }
            //}   Console.WriteLine(ci);
            //Console.ReadLine();

            //9*9口诀表
            //int b = 0;
            //int a = 1;
            //while (a <= 9)
            //{
            //    int d = 1;
            //    while (d <= a)
            //    {
            //        b = a * d;
            //        Console.Write(d + "*" + a + "=" + b + " ");
            //        d++;
            //    }
            //    Console.WriteLine();
            //    a++;
            //}
            //Console.ReadLine();

原文地址:https://www.cnblogs.com/a12110303043/p/5701034.html