C++学习小结之语句


一、顺序语句

二、条件,分支语句

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
      {
 
      }

以上所述就是本文的全部内容了,希望大家能够喜欢。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3