C#整理4——循环语句

一、循环语句 

定义:可以反复执行某段代码,直到不满足循环条件为止。

循环的四要素:初始条件、循环条件、状态改变、循环体。

1.初始条件:循环最开始的状态。

2.循环条件:在什么条件下进行循环,不满足此条件,则循环终止。

3.状态改变:改变循环变量值,最终不满足循环条件,从而停止循环。

4.循环体:要反复执行的部分。

二、for循环(重点)

1. 语法:

           for(表达式1;表达式2;表达式3)

           {

               执行语句;(循环体)

            }

2. 执行过程:

   1. 计算表达式1转向2

   2. 计算表达式2(循环条件)若为true转向3,false转向5

   3. 执行循环体转向4

   4. 执行表达式3转向2

   5. 循环结束

一般情况下:

      表达式1:用于定义循环变量和对循环变量赋初值

      表达式2:循环条件

      表达式3:用于改变循环条件的值

注意:一般情况下for循环用于已知次数的循环中。

 

foreach循环

a. 语法:

           foreach(数据类型 变量名 in 数组/集合)

           {
                        循环体;

           }

        其中in关键字前面就是定义一个迭代变量,in后面就是实现了IEnumerable 或者 IEnumerable<T> 接口的对象

   b. foreach遍历数组的简单原理:“in数组名”会将数组中的元素从第0个开始到最后一个遍历出来赋给迭代变量,所以迭代变量直接就是数组元素的值。

       注意:迭代变量的数据类型必须要与数组中元素类型一致。

   c. 执行过程:每次遍历的时候将当前遍历出来的数组元素拿出来赋给迭代变量,然后再执行循环体。

   d. for循环和foreach循环的区别与联系:

     1. 相同点:

                    都可以遍历数组中的元素。

     2. 不同点:

                   1> for循环遍历出来的是数组元素的下标,foreach遍历出来的直接就数组元素。

                   2> for循环可以从指定下标开始遍历,而foreach只能从第0个开始直到最后一个结束。

                   3> 在for循环中可以更改遍历出来元素的值,而在foreach中不能更改遍历出来的值(迭代变量的值)。

                   4> for循环中可以得到当前遍历出来的值以外的值(即上一个值和下一个值,因为for是通过遍历索引来取值的)而foreach只能得到当前遍历出来的元素值。

                        因为foreach是直接遍历数组中元素的值。

  e. 什么情况下用foreach来遍历数组中的元素?

      1. 从头开始遍历直到最后一个。

      2. 只取值不改值

while循环

1.语法:

  while(循环条件)

  {

     循环体;

  }

1. 执行过程:

  1. 先判断循环条件,如果为true。则转向2,如果为false转向语句3

  2. 执行循环体,执行完了转向1。

  3. 跳出循环循环结束。

  注意:在while循环中一定要有那么一句话来改变循环变量的值,使循环变量终有那么一个时候为false。(不然死循环)

do-while()循环

1. 语法:

          do

             {

                 //循环体

             }while(循环条件);注意:这里一定要有分号。

2.执行过程:

    1.执行循环体,执行完循环体转向2

    2.判断条件是否成立,如果条件为true则转向1 为false则转向3.

    3.跳出循环,循环结束。

3.while与do-while的区别:

        如果条件一开始就为False,对于while循环,循环体一次都不会执行。对于do-while循环体执行一次。即:while先判断后执行,do-while先执行一次再判断。

三、执行过程:

1.执行初始条件

2.执行循环条件

3.循环体

4.状态改变

5继续第2步。

 

四、for 循环的应用:迭代法,穷举法。

迭代法应用:

1.100以内所有数的和。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         //求前100个数的和
11         static void Main(string[] args)
12         {
13             int sum = 0;
14             for (int i = 1; i <= 100; i++)
15             {
16                 sum = sum + i;
17             }
18             Console.WriteLine(sum);
19         }
20     }
21 }
View Code

2.求阶乘

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class1
 9     {
10         //求阶乘   5! = 5*4*3*2*1 = 5*4!;
11         static void Main(string[] args)
12         {
13             int jc = 1;
14             for(int i=1;i<=5;i++)
15             {
16                 jc = jc * i;
17             }
18             Console.WriteLine(jc);
19         }
20     }
21 }
View Code

3.求年龄:有6个小孩子排在一起,问第一个多大年龄,他说比第二个小2岁,问第二个多大年龄,他说比第三个小2岁,以此类推,问第6个多大年龄,他说自己16岁。问第一个小孩子几岁?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     //求年龄:有6个小孩子排在一起,问第一个多大年龄,他说比第二个小2岁,问第二个多大年龄,他说比第三个小2岁,以此类推,问第6个多大年龄,他说自己16岁。问第一个小孩子几岁?
 9     class Class2
10     {
11         static void Main(string[] args)
12         {
13             int age = 16; //初始情况下,存的是第6个小孩子年龄,每次循环都会减2,分别代表第5,4,3,2,1个小孩子的年龄。
14             for (int i = 5; i >= 1; i--)
15             {
16                 age = age - 2;
17             }
18             Console.WriteLine(age);
19         }
20     }
21 }
View Code

4.折纸:a.一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,问折多少次能超过珠峰的高度?

            b.一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,折50次高度是多少?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     //一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,问折多少次能超过珠峰的高度?
 9     //一张纸的厚度是0.15毫米,假设这张纸足够大,可以无限次对折,折50次高度是多少?
10     class Class3
11     {
12         static void Main(string[] args)
13         {
14             double h = 0.00015;
15             for (int i = 1; i <= 26; i++)
16             {
17                 h = h * 2;
18             }
19             Console.WriteLine(h);
20         }
21     }
22 }
View Code

5.棋盘放粮食( 自己做)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 棋盘粮食
 9     {
10         static void Main(string[] args)
11         {
12             int sum = 1, s = 1;
13             for (int i = 1; i <= 63;i++ )
14             {
15                 s = s * 2;
16                 sum = sum+s;
17             }
18             Console.WriteLine(sum);
19 
20 
21         }
22     }
23 }
View Code

6.猴子吃桃子:公园里有一只猴子,和一堆桃子。猴子每天吃掉桃子数量的一半,把剩下的一半数量中扔掉一个坏的。到了第7天猴了睁开眼发现只剩下一个桃子了,问原来有多。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     //猴子吃桃子。
 9     //公园里有一只猴子,和一堆桃子。猴子每天吃掉桃子数量的一半,把剩下的一半数量中扔掉一个坏的。到了第7天猴了睁开眼发现只剩下一个桃子了,问原来有多。
10     //190 
11     class Class4
12     {
13         static void Main(string[] args)
14         {
15             int count = 1;
16             for(int i=6;i>=1;i--)
17             {
18                 count = (count + 1) * 2;
19             }
20             Console.WriteLine(count);
21         }
22     }
23 }
View Code

7.落球问题。(自己做)一个球从10米高度落下,每次弹起2/3的高度。问第五次弹起后的高度是多少?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class 落球问题
 9     {
10         //一个球从10米高度落下,每次弹起2/3的高度。问第五次弹起的高度是多少?
11         static void Main(string[] args)
12         {
13             double h = 10;
14             for (int i = 1; i <= 5;i++ )
15             {
16                 h = h * 2 / 3;
17             }
18             Console.WriteLine(h);
19         }
20     }
21 }
View Code

8.兔子小兔子的问题。一对新生兔,到三个月开始生一对小兔,以后每个月都会生一对小兔,小兔不断长大也会生小兔。假设兔子不死,每次只能生一对(公母),问第24末有多少只兔子?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     //兔子生兔子
 9     class Class5
10     {
11         static Main fff(string[] args)
12         {
13             int tu1 = 1, tu2 = 1; //tu1是倒数第一个月的兔子数,tu2是倒数第二个月的兔子数
14             int tu=0;//要求的这个月的兔子数。
15 
16             for (int i = 3; i <= 24; i++)
17             {
18                 tu = tu1 + tu2;
19                 tu2 = tu1;
20                 tu1 = tu;
21                 
22             }
23             Console.WriteLine(tu);
24         }
25     }
26 }
View Code

穷举法的应用:用循环把各种可能的情况都给走一遍,然后用if条件把满足要求的结果给筛选出来。

1.找100以内的与7有关的数。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             for (int i = 1; i <= 100; i++)
13             {
14 
15                 if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) //重点    
16                 {
17 
18                     Console.Write(i + "\t");
19 
20                 }
21 
22             }
23         }
24     }
25 }
View Code

2.有三种硬币若干:1分,2分,5分。要组合1毛5,有哪些组合方式?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class6
 9     {
10         static void Main(string[] args)
11         {
12             for(int a=0;a<=15;a++) //a代表1分的硬币个数
13             {
14                 for(int b=0;b<=7;b++)//b代表2分的硬币个数
15                 {
16                     for(int c=0;c<=3;c++)//c代表5分硬币的个数
17                     {
18                         if(a*1+b*2+c*5 == 15)
19                         {
20                             Console.WriteLine("1分硬币需要"+a+"个,2分的硬币需要"+b+"个,5分的硬币需要"+c+"");
21                         }
22                     }
23                 }
24             }
25         }
26     }
27 }
View Code

3.买东西。小张过元旦发了100元的购物券,他要买香皂(5元),牙刷(2元),洗发水(20元)。要想把100元正好花完,如何买这三样东西?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class 元旦
 9     {
10         //购物券100元,香皂(5元),牙刷(2元),洗发水(20元)。正好花完,怎么花?
11         static void Main(string[] args)
12         {
13             for (int a=0;a<=20;a++)
14             {
15                 for (int b = 0; b <= 50; b++)
16                 {
17                     for (int c = 0; c <= 5;c++ )
18                     {
19                         if (5*a+2*b+20*c==100)
20                         {
21                             Console.WriteLine("应该买香皂"+a+"块,牙刷"+b+"只,洗发水"+c+"瓶。");
22                         }
23                     }
24                 }
25             }
26         }
27     }
28 }
View Code

4.百鸡百钱。有100文钱,要买100只鸡回家。公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只。如何买?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class 百钱白鸡
 9     {
10         //有100文钱,要买100只鸡回家。公鸡2文钱一只,母鸡一文钱一只,小鸡半文钱一只。如何买?
11         static void Main(string[] args)
12         {
13             for (int a = 0; a <= 50;a++ )
14             {
15                 for (int b = 0; b <= 100;b++ )
16                 {
17                     for (double c = 0; c <= 200;c++ )
18                     {
19                         if(a*2+b*1+c*0.5==100&&a+b+c==100)
20                         {
21                             Console.WriteLine("公鸡"+a+"只,母鸡"+b+"只,小鸡"+c+"只。");
22                         }
23                     }
24                 }
25             }
26         }
27     }
28 }
View Code

百马百石。有100石粮食,母匹大马驮2石,每匹中马驮1石,每两匹小马驹一起驮1石。要用100匹马驮完100石粮食,如何按排?

5.某侦察队接到一项紧急任务,要求在A、B、C、D、E、F六个队员中尽可能多地挑若干人,但有以下限制条件:
A和B两人中至少去一人; a+b>=1
A和D不能一起去; a+d<=1
A、E和F三人中要派两人去; a+e+f==2
B和C都去或都不去; b+c!=1
C和D两人中去一个; c+d==1
若D不去,则E也不去。 d+e==0||d==1
问应当让哪几个人去?

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class 侦察队
 9     {
10         static void Main(string[] args)
11         {
12             for (int a = 0; a <= 1;a++ )
13             {
14                 for (int b = 0; b <= 1;b++ )
15                 {
16                     for (int c = 0; c <= 1;c++ )
17                     {
18                         for (int d = 0; d <= 1;d++ )
19                         {
20                             for (int e = 0; e <= 1;e++ )
21                             {
22                                 for (int f = 0; f <= 1;f++ )
23                                 {
24                                     if (a + b >= 1 && a + d <= 1 && a + e + f == 2 && b + c != 1 && c + d == 1 && (d + e == 0 || d == 1))
25                                     {
26                                         Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d+";e="+e+";f="+f);
27                                     }
28                                 }
29                             }
30                         }
31                     }
32                 }
33 
34             }
35         }
36     }
37 }
View Code

6.123()45()67()8()9=100;要求在()里面填写+或-使等式成立。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication4
 7 {
 8     class Class1
 9     {
10         //123()45()67()8()9=100;要求在()里面填写+或-使等式成立。
11         static void Main(string[] args)
12         {
13             for (int a = -1; a <= 1;a=a+2 )//-1代表减号,1代表加号
14             {
15                 for (int b = -1; b <= 1;b=b+2 )
16                 {
17                     for (int c = -1; c <= 1;c=c+2 )
18                     {
19                         for (int d = -1; d <= 1;d=d+2 )
20                         {
21                             if (123+a*45+67*b+8*c+9*d==100)
22                             {
23                                 Console.WriteLine("a="+a+";b="+b+";c="+c+";d="+d);
24                             }
25                         }
26                     }
27                 }
28             }
29         }
30     }
31 }
View Code

 

转载于:https://www.cnblogs.com/viven/p/4315802.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值