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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM