• for循环语句以及迭代法和穷举法


    循环语句:

    四要素:初始条件,循环条件,状态改变,循环体

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

    案例1:打印等腰直角三角形和菱形

    左上三角

     1 static void Main(string[] args)
     2         {
     3             Console.WriteLine("请输入一个数:");
     4             int n = Convert.ToInt32(Console.ReadLine());
     5 
     6         //打印左上三角形
     7             for (int i = 1; i <= n; i++)
     8             {
     9                 for (int j = 1; j <= i; j++)
    10                 {
    11                     Console.Write("");
    12                 }
    13                 Console.WriteLine();
    14             }
    15 }

    运行结果:

    左下三角:

     1 static void Main(string[] args)
     2         {
     3             Console.WriteLine("请输入一个数:");
     4             int n = Convert.ToInt32(Console.ReadLine());
     5                for (int i = 1; i <= n; i++)
     6             {
     7                 for (int j = 1; j <= n + 1 - i; j++)
     8                 {
     9                     Console.Write("");
    10                 }
    11                 Console.WriteLine();
    12             }
    13 }

    运行结果:

    右上三角:

     1  static void Main(string[] args)
     2         {
     3             Console.WriteLine("请输入一个数:");
     4             int n = Convert.ToInt32(Console.ReadLine());
     5                  //打印右上三角
     6             for (int i = 1; i <= n; i++)
     7             {
     8                 for (int j = 1; j <= n - i; j++)
     9                 {
    10                     Console.Write("  ");
    11                 }
    12                 for (int k = 1; k <= i; k++)
    13                 {
    14                     Console.Write("");
    15                 }
    16                 Console.WriteLine();
    17             }
    18 }

    运行结果:

    右下三角:

     1  static void Main(string[] args)
     2         {
     3             Console.WriteLine("请输入一个数:");
     4             int n = Convert.ToInt32(Console.ReadLine());
     5                   for (int i = 1; i <= n; i++)
     6             {
     7                 for (int j = 1; j <= i - 1; j++)
     8                 {
     9                     Console.Write("  ");
    10                 }
    11                 for (int k = 1; k <= n + 1 - i; k++)
    12                 {
    13                     Console.Write("");
    14                 }
    15                 Console.WriteLine();
    16             }
    17 }

    运行结果:

    菱形:

     static void Main(string[] args)
            {
                Console.WriteLine("请输入一个数:");
                int n = Convert.ToInt32(Console.ReadLine());
                       //打印上半部分菱形
                for (int i = 1; i <= n; i++)
                {
                    for (int j = 1; j <= n - i; j++)
                    {
                        Console.Write("  ");
                    }
                    for (int k = 1; k <= 2 * i - 1; k++)
                    {
                        Console.Write("");
                    }
                    Console.WriteLine();
                }
                
                //打印下半部分菱形
                for (int i = 2; i <= n; i++)
                {
                    for (int j = 1; j <= i - 1; j++)
                    {
                        Console.Write("  ");
                    }
                    for (int k = 1; k <= 11 - 2 * i; k++)
                    {
                        Console.Write("");
                    }
                    Console.WriteLine();
                }
                
            }

    运行结果:

    break; 完全终止循环,退出循环。 吃到苍蝇
    continue; 中断本次循环,进入下次循环。 吃到沙子

    1.迭代法: - 有规律可寻

    //100以内所有数的和。

     1  static void Main(string[] args)
     2         {
     3              // 求100以内所有数的和
     4             //收公粮
     5             int sum = 0;
     6 
     7             for (int i = 1; i <= 100; i++)
     8             {
     9                 sum = sum + i;
    10             }
    11             Console.WriteLine(sum);
    12      }       

    //猴子吃桃子

    公园里有一只猴子,和一堆桃子,猴子每天吃完桃子总数的一半,在剩下一半数量中扔掉一个坏的。每天这样吃,到第七天,猴子睁开眼时,发现只剩下一个桃子了,问刚开始公园里有多少个桃子? 190

     1  public static void Main(string[] args)
     2         {
     3            //猴子吃桃子
     4             int taozi = 1;
     5             for (int i = 6; i >= 1; i--)
     6             {
     7                 taozi = (taozi + 1) * 2;
     8             }
     9 
    10             Console.WriteLine(taozi);
    11         }

    //国象棋盘放米

     1 public static void Main(string[] args)
     2         {
     3                  //国象放米
     4             double mi = 1;
     5             Console.Write(mi + "	");//第一行的米数
     6 
     7             for (int i = 2; i <= 64; i++)
     8             {
     9                 mi = mi * 2;
    10                 Console.Write(mi + "	");
    11             }
    12         }

    结果:

    //拆纸多少次就比珠峰高 8848米

    一张A4纸的厚度:0.088毫米 =0.0088厘米 = 0.00088分米=0.000088米

     1 public static void Main(string[] args)
     2         {
     3              double houdu = 0.000088;
     4 
     5             for (int i = 1; ; i++)
     6             {
     7                 houdu = houdu * 2;
     8                 Console.Write(houdu + "	");
     9                 if (houdu > 8848)
    10                 {
    11                     Console.WriteLine("对折了{0}次后就超过珠峰了", i);
    12                     break;
    13                 }
    14             }
    15        }

    运行结果:

    2.穷举法 :

    用循环,把所有可能的情况都走上一遍,然后使用if过滤出满足条件的情况来。

    //100以内所有与7有关的数。

     1  static void Main(string[] args)
     2         {
     3             #region ====求100以内与7有关的数====
     4 
     5             for (int i = 1; i <= 100; i++)
     6             {
     7                 if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
     8                 {
     9                     Console.Write(i + "	");
    10                 }
    11             }
    12            #endregion
    13 }

    //百鸡百钱
    公鸡2文钱,母鸡1文钱,小鸡半文钱。用100文钱,买100只鸡,每类鸡只少买1只,有哪几种组合?

     1  static void Main(string[] args)
     2         { 
     3         //百鸡百钱:公鸡2文,母鸡1文,小鸡半文,每种至少一只,100文买100只鸡有多少可能性
     4             int sum = 0;
     5             for (int a = 1; a <=50; a++)
     6             {
     7                 for (int b =1; b <100; b++)
     8                 {
     9                     for (int c = 1; c <=100; c++)
    10                     {
    11                         if (a+b+c==100&&a*2+b*1+c*0.5==100)
    12                         {
    13                             sum++;
    14                             Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只",a,b,c);
    15                         }
    16                     }
    17                 }
    18             }
    19             Console.WriteLine("一共有{0}种可能性",sum);
    20         }

    123( )45( )67( )8(  )9=100,括号里填+或-使得等式两边相等。

     1  public static void Main(string[] args)
     2         {
     3 
     4             for (int a = -1; a <= 1; a = a + 2)
     5             {
     6                 for (int b = -1; b <= 1; b = b + 2)
     7                 {
     8                     for (int c = -1; c <= 1; c = c + 2)
     9                     {
    10                         for (int d = -1; d <= 1; d = d + 2)
    11                         {
    12                             if (123 + 45 * a + 67 * b + 8 * c + 9 * d == 100)
    13                             {
    14                                 Console.Write("{0},{1},{2},{3}", a, b, c, d);
    15                             }
    16                         }
    17                     }
    18                 }
    19             }
    20         }

    小张单位发100元的购物卡,小张要去超市买三种日常用品:牙刷(5元)、香皂(2元)、洗发水。(15元),购物卡不退现,小张又不想多花钱,如何购买刚好花完这100元的卡?

     1 static void Main(string[] args)
     2         {
     3            
     4              int count=0;
     5             for (int a = 0; a <=20; a++)
     6             {
     7                 for (int b = 0; b <=50; b++)
     8                 {
     9                     for (int c = 0; c <=6; c++)
    10                     {
    11 
    12                         if (a*5+b*2+c*15==100)
    13                         {
    14                             count++;
    15                             Console.WriteLine("牙刷{0}只,香皂{1}个,洗发水{2}瓶", a, b, c);
    16                         }
    17                         
    18                     }
    19                 }
    20             }
    21             Console.WriteLine("一共有{0}种可能性恰好花光100元",count);
    22         }

     //打印出所有的水仙花数,所谓的水仙花数就是一个三位数,其余各个数字的立方和等于它的本身,例如153是一个水仙花数,153=1∧3+5∧3+3∧3

     1 static void Main(string[] args)
     2         { 
     3            //打印出所有的水仙花数,所谓的水仙花数就是一个三位数,其余各个数字的立方和等于它的本身,例如153是一个水仙花数,153=1∧3+5∧3+3∧3
     4             for (int i = 111; i <=999; i++)
     5             {
     6                 int a = i / 100;//取出三位数的百位
     7                 int b = i / 10 % 10;//取出三位数的十位
     8                 int c = i % 10;//取出三位数的个位
     9 
    10                 if (a*a*a+b*b*b+c*c*c==i)
    11                 {
    12                     Console.WriteLine(i);
    13                 }
    14 
    15             }
    16 
    17         }

    运行结果:

  • 相关阅读:
    在列表中添加序号列
    在C#中使用正则表达式
    Git
    Linux 配置Java环境
    讯飞语义理解 JAVA SDK
    分屏显示
    Gdiplus
    重启进程
    MFC 常用功能属性
    MFC 打印
  • 原文地址:https://www.cnblogs.com/kellybutterfly/p/5402455.html
Copyright © 2020-2023  润新知