C#控制台应用程序——穷举&迭代

穷举和迭代需要用到循环语句及其嵌套。

循环


初始条件,循环条件,状态改变,循环体。


for(初始条件;循环条件;状态改变)
{
    循环体
}

打印圆点
  1 namespace yuandian
  2 {
  3     class Program
  4     {
  5         static void Main(string[] args)
  6         {
  7             ////5x5
  8 
  9             //for (int i = 1; i <= 5; i++)
 10             //{
 11             //    for (int a = 1; a <= 5; a++)
 12             //    {
 13             //        Console.Write("●");
 14             //    }
 15             //    Console.WriteLine();
 16             //}
 17 
 18             ////54321 左对齐
 19 
 20             //for (int i = 1; i <= 5;i ++ )
 21             //{
 22             //    for (int a=1;a<=6-i;a++)
 23             //    {
 24             //    Console.Write ("●");
 25             //    }
 26             //    Console.WriteLine();
 27             //}
 28 
 29             ////12345 左对齐
 30 
 31             //for (int i = 1; i <= 5; i++)
 32             //{
 33             //    for (int a = 1; a <= i; a++)
 34             //    {
 35             //        Console.Write("●");
 36             //    }
 37             //    Console.WriteLine();
 38             //}
 39 
 40             ////12345 右对齐
 41 
 42             //for (int a = 1; a <= 5; a++)
 43             //{
 44             //    for (int b = 1; b <= 5 - a; b++)
 45             //    {
 46             //        Console.Write("  ");
 47             //    }
 48             //    for (int c = 1; c <= a; c++)
 49             //    {
 50             //        Console.Write("●");
 51             //    }
 52             //    Console.WriteLine();
 53             //}
 54 
 55             ////54321右对齐
 56 
 57             //for (int a = 1; a <= 5; a++)
 58             //{
 59             //    for (int b = 1; b <= a-1 ; b++)
 60             //    {
 61             //        Console.Write("  ");
 62             //    }
 63             //    for (int c = 1; c <= 6-a; c++)
 64             //    {
 65             //        Console.Write("●");
 66             //    }
 67             //    Console.WriteLine();
 68             //}
 69 
 70             ////12345居中
 71 
 72             //for (int a = 1; a <= 4;a++ )
 73             //{
 74             //    for (int b = 1; b <= 4-a;b++ )
 75             //    {
 76             //        Console.Write("  ");
 77             //    }
 78             //    for (int c = 1; c <= 2*a-1;c++ )
 79             //    {
 80             //        Console.Write("●");
 81             //    }
 82             //    Console .WriteLine();
 83             //}
 84 
 85             //54321居中
 86 
 87             for (int a = 1; a <= 5;a++ )
 88             {
 89                 for (int b=1;b<=a-1;b++ )
 90                 {
 91                  Console .Write("  ");                
 92                 }
 93                 for (int c=1;c<=9-2*a;c++ )
 94                 {
 95                 Console .Write ("");
 96                 }
 97                 Console.WriteLine();
 98             }
 99         }
100     }
101 }
求100以内所有数的和。
 1 namespace qiu100neizhengshudehe
 2 {
 3     class Program1
 4     {
 5         static void Main(string[] args)
 6         {
 7             int sum = 0;
 8             for (int i = 1; i <= 100;i++ )
 9             {
10                 sum = sum + 1;
11             }
12             Console.WriteLine(sum);
13         }
14     }
15 }
 100以内与7有关的数。
 1 namespace _7
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             for (int a = 1; a <= 100; a++)
 8             {
 9                if (a%7==0||a%10==7||a/10==7)
10                 {
11                     Console.Write(a+"	");
12                 }
13             }
14         }
15     }
16 }

穷举

 1 namespace BaiJiBaiQian
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             for (int a = 1; a <= 50;a++ )
 8             {
 9                 for (int b = 1; b <= 100;b++ )
10                 {
11                     for (float  c = 1; c <= 100;c++ )
12                     {
13                         if (a*2+b*1+c *0.5==100)
14                         {
15                             Console.WriteLine("公鸡可以买" + a + "只," + "母鸡可以买" + b + "只," + "小鸡可以买" + c + "只,");
16                         }
17                     }
18                 }
19             }
20         }
21     }
22 }
百鸡百钱

  

 1 namespace BaiMaBaiDan
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             for (int a = 1; a <= 50;a++ )
 8             {
 9                 for (int b = 1; b <= 100;b++ )
10                 {
11                     for (float  c = 1; c <= 100;c++ )
12                     {
13                         if (a*2+b*1+c*0.5==100)
14                         {
15                             Console.WriteLine("大马需要" + a + "匹," + "中马马需要" + b + "匹," + "小马需要" + c + "匹,");
16                         }
17                     }
18                 }
19             }
20         }
21     }
22 }
百马百担
 1 namespace GouWuKa
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             for (int a = 1; a <= 6;a++ )
 8             {
 9                 for (int b = 1; b <= 50;b++ )
10                 {
11                     for (int c = 1; c <= 20;c++ )
12                     {
13                         if (15*a +2*b +5*c ==100)
14                         {
15                             Console.WriteLine("洗发水可以买" + a + "件," + "香皂可以买" + b + "件," + "牙膏可以买" + c + "件,");
16                         }
17                     }
18                 }
19             }
20         }
21     }
22 }
购物卡
 1 namespace YingBi
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             for (int a = 1; a <= 100;a++ )
 8             {
 9                 for (int b = 1; b <= 50;b++ )
10                 {
11                     for (int c = 1; c <= 20;c++ )
12                     {
13                         if (a * 1 + b * 2 + c * 5 == 100)
14                         {
15                             Console.WriteLine("一分钱硬币需要"+a+"枚,"+"二分钱硬币需要"+b+"枚,"+"五分钱硬币需要"+c+"枚,");
16                         }
17                     }
18                 }
19             }
20         }
21     }
22 }
硬币
 1 namespace ZhenChaBing
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             for (int a = 0; a <= 1; a++)
 8             {
 9                 for (int b = 0; b <= 1; b++)
10                 {
11                     for (int c = 0; c <= 1; c++)
12                     {
13                         for (int d = 0; d <= 1; d++)
14                         {
15                             for (int e = 0; e <= 1; e++)
16                             {
17                                 for (int f = 0; f <= 1; f++)
18                                     if (a + b >= 1 && a + d != 2 && a+e+f==2&&b + c != 1 && c + d == 1 && (d + e == 0 || d == 1))
19                                     {
20                                         Console.WriteLine("a=" + a + "	" + "b=" + b + "	" + "c=" + c + "	" + "d=" + d + "	" + "e=" + e + "	" + "f=" + f);
21                                     }
22                             }
23                         }
24                     }
25                 }
26             }
27         }
28     }
29 }
侦察兵

迭代

原文地址:https://www.cnblogs.com/Kekeluotu/p/4482396.html