C++ learning summary statement

  • 2020-04-02 03:08:11
  • OfStack

One, sequential statement

Two, condition, branch statement

1. If statement

The key is to be able to skillfully use if nesting. Consider all the circumstances.

If the condition corresponds to each other, you can just use if and else. But you have to figure out which if is paired with each else.

If the situation is more than three independent cases, then you can choose to use if... Else if... The else.

1. If statements

If (condition)
{
When conditions are met;
}

2. If (condition)

{
To meet the conditions of execution;
}
The else
{
If the conditions are not met;
}

3 if(condition 1)
{
When condition 1 is satisfied;
}
Else if(condition 2)
{
If condition 1 is not satisfied, condition 2 is satisfied;
}

4.

If conditions (1)
{
If conditions (2)
{
If both condition 1 and condition 2 are satisfied;
}
}

2. Switch statement

If there are more conditions to choose from, the switch statement is more efficient than the if statement. Special attention is paid to the break following the case.

Eg:

  / / eg. 6 switch beyond constant-like statements     scope
              The static void Maine (string [] args)
              {
                      // console.writeline (" the hero you have chosen to appear is: ");
                      Random r = new Random();
                      Int n = r.N ext (10);

                      String a;

                      The switch (n)
                      {
                              Case 1:
                                      A = "zhao xin";       Break;
                              Case 2:
                                      A = "ice shooter "; Break;
                              Case 3:
                                      A = "infinity kensei "; Break;
                              Case 4:
                                      A = "robot ";   Break;
                              Default:
                                      A = "qi tian da sheng "; Break;
                      }
                      Console.WriteLine(" the selected hero is: "+a ");
              }

Loop statement

The for loop

Four elements:

Initial condition, loop condition, state change, loop body. Execution process:

Initial condition - loop condition - loop body - state change - loop condition...

Note: the semicolon is separated from the bracket for, and the semicolon is not added after the bracket for.

By adding breakpoints, you can better understand how for works.

1. For the instance of the loop empty operation completed, output the number within 100


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

You can also use while, if() break; This is done by nesting.

. Inference of positive and reverse orders. (origami problem)

  //eg.5 origami problem


     static void Maine(string[] args)
     {
       //Console.WriteLine(" please enter times ");
       //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. exhaustive method: loop through all possible situations and then use the if condition to filter out the results that meet the requirements.

  // a hundred horses and a hundred stones can carry two stones on a big horse and a small horse can carry one stone on a small horse  
 


    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(" Malaysia need " + i + " The head, the horse needs " + j + " Head, pony needs " + k + " Head. ");
             }
           }
         }
       }
     }

                / / 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 Yuan to buy " + i.ToString() + " A toothbrush, " + j.ToString() + " A toothpaste, " + k.ToString() + " A bar of soap, just enough. ");
             }
           }
         }
       }
 
     }

                //eg.8 you have one, two, five dollars, how much, scrape up twenty dollars, there are several ways to scrape up twenty dollars


     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(" A total of " + m + " In the method. ");
               Console.WriteLine(" Need to be 1 yuan " + i + " Zhang, 2 yuan " + j + " Zhang, 5 yuan " + k + " Zhang. ");
             }
           }
         }
       }
     }

                / / eg. 9   1 () 2 () 3 () 4 = 4; I'm going to put (- or +) in parentheses.


     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 () () () (9) = 100; To make the equation true, you have to write either + or - in ().


     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 an investigation team was given an urgent task to select as many members as possible from the six members of A.B.C,D,E and F. A and B must go together. A and D cannot go together. A,E and F three have to go by two. B and C
                // go or not at all. One of C and D. If D doesn't go, neither does E. Who should be sent? (flexible use of 1 and 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);
                   }
 
                 }
               }
             }
           }
         }
       }
     }
     //The teacher edition
     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. Iterative method: there are certain rules. Each loop gets data from the last operation, and the result of this operation is to prepare for the next operation.

Eg1 rabbit problem

There is a pair of young rabbits, young rabbits after a month to grow into a rabbit, rabbit after a month to grow into a rabbit and give birth to a pair of young rabbits, ask a few years later how many pairs of rabbits, which rabbit, rabbit, rabbit is how much?

//eg.2 the rabbit problem


    //Methods a
    static void Maink3(string[] args)
    {
      int syt = 1, byt = 0;
      int sxt = 0, bxt = 0;
      int sct = 0, bct = 0;

      Console.WriteLine(" Please enter the number of months: ");
      int month = Convert.ToInt32(Console.ReadLine());
      int sum;

      for (int i = 1; i <= month; i++)
      {
        //The order of assignment cannot be changed, it must follow the growth rule of rabbit, the first BCT will have byt
        bct = sxt + sct;
        bxt = syt;
        byt = sxt + sct;

        //BCT = SXT + SCT; Write it this way. You have to pay attention to his order
        //bxt = syt;
        //byt = bct;



        //byt = bct;// The wrong 
        //bxt = syt;
        //bct = sxt + sct; 

        syt = byt;
        sxt = bxt;
        sct = bct;

        //sum = byt + bxt + bct;
      }

      sum = byt + bxt + bct;

      Console.WriteLine(" After the {0} After months, the number of young rabbits is {1} Yes, the number of rabbits is zero {2} Yes, the number of rabbits is {3} Yes, there are {4} right ", month.ToString(), byt, bxt, bct,sum);
     
    }
    //Method 2
    static void Maink4(string[] args)
    {
      int n = Convert.ToInt32(Console.ReadLine());
      int tu = 0;//Ask for the total for that month
      int tu1=1, tu2=1;//The first to last is tu1, and the second to last is tu2

      for (int i = 3; i < n;i++ )
      {
        tu = tu1 + tu2;
        tu2 = tu1;
        tu1 = tu;
      }
      Console.WriteLine(tu);

    }
    //Methods three
    static void Maink5(string[] args)
    {
      Console.Write(" Please enter the number of months: ");
      int m = int.Parse(Console.ReadLine());
      int ct = 0;//The log of a rabbit
      int xt = 0;//The log of a rabbit
      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() + " After one month The log of a rabbit Is this: " + ct.ToString());
        Console.WriteLine(i.ToString() + " After one month The log of a rabbit Is this: " + xt.ToString());
        Console.WriteLine(i.ToString() + " The logarithm of the young rabbit is: " + yt.ToString());
        Console.WriteLine(i.ToString() + " The total logarithm of rabbits after months is: " + zt.ToString());
      }
      Console.ReadLine();
    }

Eg. 2   The sum of all the Numbers up to 100.

Eg3. Take the factorial.
Eg4. Find the age.
Eg5. Origami.
Eg6. Grain on a chessboard.
The monkey eats the peach.


 static void Maink(string[] args)
    {
      int sum = 1;
      for (int i = 0; i < 6; i++)
      {
        int t = (sum + 1) * 2;
        sum = t;
      }
      Console.WriteLine(" Peaches in all: " + sum + " A. ");
    }

Eg8. Ball drop problem. A ball drops from a height of 10 meters, each bounce 2/3 of the height, after the fifth bounce?

While loop. It's used in some dead loops.

Try catch. Protect the program from failure when the program fails.

Format:


 try//Shortcut: double click the TAB key
      {

      }
      catch (Exception)
      {

        throw;
      }
      finally
      {
 
      }

The above is all the content of this article, I hope you can enjoy it.


Related articles: