7.24

循环语句

for 嵌套  穷举 迭代

直角在右下角的 三角形

Console.Write("请输入一个数");
            int n = int.Parse(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                for (int t = 1; t <= n - i; t++)
                {
                    Console.Write("  ");
                }
                for (int k = 1; k <= i; k++)
                {
                    Console.Write("■");
                }
                Console.WriteLine();
            }
            Console.ReadLine();

穷举

百马百担

大马两石粮食 中马 1石粮食 两头小马托1石粮食,
           
   100匹马 托100石粮食 如何分配
            int n = 0;
            for (int d = 1; d * 2 <= 100; d++)
            {
              for (int z = 1; z * 1 <= 100; z++)
               {
                   for (int x = 1; x * 0.5 <= 100; x++)
                   {
                       if (d * 2 + z * 1 + x * 0.5 == 100 && d + z + x == 100)
                       {
                            Console.WriteLine(d + "匹大马" + z + "匹中马" + x + "匹小马" + (d + z + x) + "粮食");
                            n++;
                       }
                    }
                }
            }
           Console.Write("有"+n+"可能性");
            Console.ReadLine();

五个小朋友,第一个比第二个大两岁 第二个比第三个大两岁,以此类推,第五个小朋友3sui,那第一个小朋友几岁

            int y = 3;
            int c = 1;
            for (; ; )
            {
                y += 2;
                c++;
                if (c == 5)
                {
                    break;
                }
            }
            Console.Write("年龄是" + y);
            Console.ReadLine();

            1分 2分 5分 硬币 组合1.5元 几种方式 分别多少个

            int n = 0;
            for (int a = 0; a <= 150; a++)
            {
                for (int b = 0; b <= 75; b++)
                {
                    for (int c = 0; c <= 30; c++)
                    {
                        if (a + b * 2 + c * 5 ==150)
                        {
                            Console.WriteLine(a+"个硬币"+b+"个硬币"+c+"个硬币");
                            n++;
                        }
                    }
                }
            }
            Console.Write("多少种可能性"+n);
            Console.ReadLine();

while

原文地址:https://www.cnblogs.com/power8023/p/5700959.html