国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++學習小結之語句

2020-01-26 15:03:31
字體:
來源:轉載
供稿:網友

一、順序語句

二、條件,分支語句

1、if語句

關鍵是能夠熟練運用 if的嵌套。要考慮好所有的情況。

如果說 條件是兩種情況相互對應的,那么就可以只用 if 與else 。但必須要想好 每個else 跟哪個if是一對。

如果情況是相互獨立的三種情況以上,那么可以選擇運用if ... else if ...else。

1.if語句

if(條件)
{
滿足條件的時候執行;
}

2. if(條件)

{
滿足條件執行;
}
else
{
不滿足條件時執行;
}

3 if(條件1)
{
滿足條件1的時候執行;
}
else if(條件2)
{
不滿足條件1的情況下滿足條件2;
}

4.

if(條件1)
{
if(條件2)
{
既滿足條件1又滿足條件2的時候執行;
}
}

2、switch 語句

如果說可選的條件比較多時,選擇switch語句,要比if語句效率要高。特別注意的是 case 后跟的break。

eg:

 //eg.6 swtich語句   作用域
        static void Maine(string[] args)
        {
            //Console.WriteLine("你本次選擇出場的英雄是:");
            Random r = new Random();
            int n = r.Next(10);

            string a;

            switch (n)
            {
                case 1:
                    a = "趙信";    break;
                case 2:
                    a = "寒冰射手";break;
                case 3:
                    a = "無極劍圣";break;
                case 4:
                    a = "機器人";  break;
                default:
                    a = "齊天大圣";break;
            }
            Console.WriteLine("本次選擇的英雄是:"+a);
        }

三、循環語句

for循環

四要素:

初始條件,循環條件,狀態改變,循環體。 執行過程:

初始條件--循環條件--循環體--狀態改變--循環條件....

注意:for的小括號里面分號隔開,for的小括號后不要加分號。

利用 加斷點的方式,可以更好的明白for的工作原理。

1.for循環空操作完成的實例, 輸出100以內的數字

 static void Main(string[] args)     {       int i = 1;       for (; ; )       {         if (i > 100)         {           break;         }         Console.Write(i + "/t");         i++;       }       Console.ReadKey();     }

當然你也可以用 while,if() break;的嵌套完成上述操作。

.正序和逆序的推斷問題。 (折紙問題)

  //eg.5 折紙問題

     static void Maine(string[] args)     {       //Console.WriteLine("請輸入次數");       //int n = Convert.ToInt32(Console.ReadLine());         //int i = 0;       //for (double sum = 0.0001; sum <= 8848.0; sum = sum * 2)       //{       //  i++;        //}       //Console.WriteLine(i);        double sum = 0.0001;       int z = 0;        for (int i = 0; ; i++)       {         z++;         sum = sum * 2;          if (sum >= 8848.0)         {           Console.WriteLine(z);           break;         }       }     }

.應用:a.窮舉法: 用循環把各種可能的情況都給走一遍,然后用if條件把滿足要求的結果給篩選出來。

 //eg.6 百馬百石 大馬馱2石,中馬馱1石 小馬馱0.5石 
 

    static void Main6a(string[] args)     {       for (int i = 0; i <= 50; i++)       {         for (int j = 0; j <= 100; j++)         {           for (int k = 0; k <= 200; k++)           {             if ( (i * 2 + j * 1 + k * 0.5 == 100) && (i + j + k == 100) )             {               Thread.Sleep(50);               Console.WriteLine("大馬需要" + i + "頭,中馬需要" + j + "頭,小馬需要" + k + "頭。");             }           }         }       }     }

         //eg.7 

     static void Maing(string[] args)     {       for (int i = 1; i < 10; i++)       {         for (int j = 1; j < 5; j++)         {           for (int k = 1; k < 25; k++)           {             if (i * 5 + j * 10 + k * 25 == 50)             {               Console.WriteLine("50元用來買" + i.ToString() + "個牙刷," + j.ToString() + "個牙膏," + k.ToString() + "塊肥皂,正好能用完。");             }           }         }       }      }

         //eg.8 有1塊,2塊,5塊的錢若干,湊出20塊錢,有幾種湊法

     static void Mainh(string[] args)     {       int m = 0;       for (int i = 0; i <= 20; i++)       {         for (int j = 0; j <= 10; j++)         {           for (int k = 0; k < 4; k++)           {             if (i * 1 + 2 * j + 5 * k == 20)             {               m++;               Console.WriteLine("一共有" + m + "中方法。");               Console.WriteLine("需要1元的" + i + "張,2元的" + j + "張,5元的" + k + "張。");             }           }         }       }     }

         //eg.9  1 () 2 () 3 ()4 = 4;問括號里我要填 (- 或 +)

     static void Maini(string[] args)     {       for (int i = 1; i <= 1; i += 2)       {         for (int j = -1; j <= 1; j += 2)         {           for (int k = -1; k <= 1; k += 2)           {             for (int l = -1; l <= 1; l += 2)             {               if (1 * i + 2 * j + 3 * k + l * 4 == 4)               {                 Console.WriteLine("i=" + i + ",j=" + j + ",k=" + k + ",l=" + l + "。");               }             }             }         }       }     }

         //eg.10  123()45()67()8()9=100;要求在()里面填寫+或-使等式成立。

     static void Maini2(string[] args)     {       for (int a = -1; a <= 2; a += 2)       {         for (int b = -1; b <= 2; b += 2)         {           for (int c = -1; c <= 2; c += 2)           {             for (int d = -1; d <= 2; d += 2)             {               if (123 + a * 45 + b * 67 + c * 8 + d * 9 == 100)                 Console.WriteLine("a=" + a + ",b=" + b + ",c=" + c + ",d=" + d);             }           }         }       }       Console.ReadKey();     }

         //eg.11 某偵查隊接到一項緊急任務,要求在A.B.C,D,E,F六名隊員中盡可能多的挑選若干人。A和B兩人必須去一人。A和D不能同時去。A,E,F三人必須兩人去。B和C都
         //去或都不去。C和D兩人中去一人。若D不去,E也不去。問應叫哪幾個人去?(靈活運用1與0)

     static void Mainj(string[] args)     {       for (int a = 0; a <= 1; a++)       {         for (int b = 0; b <= 1; b++)         {           for (int c = 0; c <= 1; c++)           {             for (int d = 0; d <= 1; d++)             {               for (int e = 0; e <= 1; e++)               {                 for (int f = 0; f <= 1; f++)                 {                   if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && (d - e >= 0))                   {                     Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);                   }                  }               }             }           }         }       }     }     //老師版     static void Mainj1(string[] args)     {       int a, b, c, d, e, f;       for (a = 0; a < 2; a++)       {         for (b = 0; b < 2; b++)         {           for (c = 0; c < 2; c++)           {             for (d = 0; d < 2; d++)             {               for (e = 0; e < 2; e++)               {                 for (f = 0; f < 2; f++)                 {                   if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && ((d + e == 0) || d == 1))                   {                     Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);                   }                 }               }             }           }         }       }       Console.ReadKey();     }

b.迭代法:有一定規律。 每次循環都是從上次運算結果中獲得數據,本次運算的結果都是要為下次運算做準備。

eg1 兔生兔問題

有一對幼兔,幼兔一個月后成長為小兔,小兔一個月后成長為成兔并生下一對幼兔,問幾年后有多少對兔子,其中幼兔,小兔,成兔分別是多少?

//eg.2 兔生兔問題

    //方法一    static void Maink3(string[] args)    {      int syt = 1, byt = 0;      int sxt = 0, bxt = 0;      int sct = 0, bct = 0;      Console.WriteLine("請輸入月數:");      int month = Convert.ToInt32(Console.ReadLine());      int sum;      for (int i = 1; i <= month; i++)      {        //賦值順序不能變,必須按照兔子生長規律來,先有的bct才會有byt        bct = sxt + sct;        bxt = syt;        byt = sxt + sct;        //bct = sxt + sct; 這樣寫,必須注意他的順序        //bxt = syt;        //byt = bct;        //byt = bct;//錯誤的        //bxt = syt;        //bct = sxt + sct;         syt = byt;        sxt = bxt;        sct = bct;        //sum = byt + bxt + bct;      }      sum = byt + bxt + bct;      Console.WriteLine("過了{0}個月后,幼兔個數為{1}對,小兔個數為{2}對,成兔個數為{3}對,總共有{4}對。", month.ToString(), byt, bxt, bct,sum);         }    //方法二    static void Maink4(string[] args)    {      int n = Convert.ToInt32(Console.ReadLine());      int tu = 0;//要求那個月的總數      int tu1=1, tu2=1;//倒數第一個為 tu1,倒數第二個為 tu2      for (int i = 3; i < n;i++ )      {        tu = tu1 + tu2;        tu2 = tu1;        tu1 = tu;      }      Console.WriteLine(tu);    }    //方法三    static void Maink5(string[] args)    {      Console.Write("請輸入月數:");      int m = int.Parse(Console.ReadLine());      int ct = 0;//成兔的對數      int xt = 0;//小兔的對數      int yt = 1;//      int zt = 1;//      for (int i = 1; i <= m; i++)      {        if (i == 1)        {          ct = 0;          xt = 0;          yt = 1;        }        else        {          ct = xt + ct;          xt = yt;          yt = ct;        }        zt = yt + xt + ct;                Console.WriteLine(i.ToString() + "個月后成兔的對數是:" + ct.ToString());        Console.WriteLine(i.ToString() + "個月后小兔的對數是:" + xt.ToString());        Console.WriteLine(i.ToString() + "個月后幼兔的對數是:" + yt.ToString());        Console.WriteLine(i.ToString() + "個月后兔子的總對數是:" + zt.ToString());      }      Console.ReadLine();    }

eg 2  100以內的所有數的和。

eg3. 求階乘。
eg4.求年齡。
eg5.折紙。
eg6.棋盤放糧食。
eg7.猴子吃桃子。

 static void Maink(string[] args)    {      int sum = 1;      for (int i = 0; i < 6; i++)      {        int t = (sum + 1) * 2;        sum = t;      }      Console.WriteLine("桃子一共有:" + sum + "個。");    }

eg8.落球問題。一個球從10米高度落下,每次彈起2/3的高度,問第五次彈起后的高度?

四、while 循環。一般用在一些死循環當中。

五、try catch。保護程序,避免程序出錯時無法運行。

格式:

 try//快捷方式:雙擊 tab鍵                                          {      }      catch (Exception)      {        throw;      }      finally      {       }

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贡觉县| 新闻| 扬州市| 洪洞县| 瑞昌市| 太仓市| 南皮县| 哈尔滨市| 界首市| 鄯善县| 来安县| 清新县| 正宁县| 庆安县| 澎湖县| 文安县| 绥阳县| 象州县| 余庆县| 广宗县| 敖汉旗| 逊克县| 本溪市| 扎囊县| 全南县| 富裕县| 九台市| 湖南省| 丹巴县| 根河市| 东阳市| 砀山县| 左权县| 蓬安县| 康定县| 锦州市| 金华市| 会同县| 乌苏市| 阿鲁科尔沁旗| 偃师市|