web123456

C Language Chapter 3 Algorithms and Control Statements Highlights Typical Questions

I. Concepts

 

 

 

 

II. Focus codes (books)

1. Enter the year to determine whether it is a leap year.

2. Enter the coefficients a, b, and c of the quadratic equation ax^2 + bx + c = 0 from the keyboard and find its roots.

3. Enter the year and month, and find the number of days in the month

4. Counting the number of students taking the test and finding the highest, lowest and average scores

5, input a positive integer, it will be inverted to form a new number of output (such as input 12345, composed of 54321 output)

6. Enter two integers from the keyboard and find their greatest common divisor

7. calculate 1!+2!+... +n!

8. Enter a positive integer from the keyboard and determine whether the number is prime or not

9. Find the number of all daffodils

10、Output the nine-nine multiplication table

11, a hundred money to buy a hundred chickens, 100 yuan to buy 100 chickens, hen 5 yuan a chicken, rooster 3 yuan a chicken, chick 0.5 yuan a chicken, asked each kind of chicken must be bought in the case of the calculation of all the purchase methods

12、Output the number between 100~200 that cannot be divided by 3

13, the output of the radius of 1 ~ 10 of the area of the circle, if the area of more than 100, the output is not

14. Find the first 40 terms of the Fibonacci series, which has the following generalized formula:

15. Determine whether a positive integer is an iambic number. An iambic number is a number such that a positive integer reads the same from left to right and from right to left (e.g. 121, 123321)

16. Find the fractional series 2/1, -3/2, 5/3, -8/5,... Sum of the first 20 terms

17, Find the follow of the quadratic higher order equation 2x^3-4x^2+3x+6=0 near 1.5

III. Topics of focus

1、logical operatorThe data type of the two-sided arithmetic object is ()

A . Just 0 or 1
B . Can only be 0 or non-zero positive
C . Can only be integer orcharacter typedigital
D . Can be any legal type of data

Answer: D

Ans: The objects on either side of a logical operator can be constants, variables, expressions. If the value of the operation object is a non-zero value, it means that the condition is true; if it is 0, it means that the condition is false, so the value of the operation object can be any data type. Logical operators on both sides of the final conversion to bool value operation, bool value is only non-0 (expressed as 1) and 0 two values, so as long as it is not 0, it is converted to 1, so no matter what type of value can be involved in the operation.

2. There is the following program fragment:

       int k=2;

       while(k=0){

             printf("%d",k);

             k--;

         }

Then the following statement is correct ( )

A. Cycle of 10 executions

B. The loop body executes an infinite number of times

C. The loop body is not executed once

D. The loop body is executed once

Answer: C

Ans: The expression k=0 in the parentheses after while is an assignment expression, i.e., the value of the expression is 0, and the loop body is not executed, so C should be chosen.

3. The number of cycles in the following program segment is ( )

      for(i=2;i==0)

           printf("%d",i--);

A. Unlimited

B. 0

C. 1 time

D. 2

Answer: B

Ans: The initial value of i is 2. The conditional expression is to determine whether i is equal to 0. Here the value of the expression is 0 (the condition is false) and jumps out of the loop.

4、The output of the following program is ( )

      #include<>
      int main()
      {
            int k=0,m=0,i,j;
            for(i=0;i<2;i++)
            {
                  for(j=0;j<3;j++)
                        k++;
                  k-=j;
         }
         m=i+j;
         printf("k=%d,m=%d",k,m);
         return 0;
    }

    A. k=0,m=3

    B. k=0,m=5

    C. k=1,m=3

    D. k=1,m=5

Answer: B

Ans: The outer loop is executed 1 time, the inner loop is executed 3 times, after 3 times the inner loop is executed k=3,j=3, k=0 after k-=j;i.e. after each execution of the inner loop the value of k is 0, and after completion of the outer loop i=2, hence m=5,k=0.

5. Compound statements are made withA pair of curly brackets {}defined block of statements

6. For an expression to constitute a C statement, it must be written insemicolon;end

7. Write the math equation The C expression of they=x<0?1:x==0?0:-1

8. The syntax of C language stipulates that: when omitting the compound statement symbol, the else statement is always the same as theCombination of nearest and unpaired ifs, independent of the writing format.

9. In the switch statement, if there is no expression with an equal value marker and there is a default marker, then the value from thedefault labelStarts execution until the switch statement ends.

10、C language loop statements includewhiledo...whileforThree.

11、The loop statement that executes the loop body at least once isdo...whileStatements.

12、continue statement's role is to end thethis timeLoop.

13, break statement can make the program flow out of the switch statement body, but also can be abortedsearchableCirculators.

14. The following program runs as   1   

        #include<>
        int main()
        {
             int i=5;
             do
             {
                   switch(i%2)
                   {
                         case 0 : i--; break;
                         case 1 : i--; continue;
                    }
                    i--;i--;
                    printf("%d",i);
             }while(i>0);
             return 0;
         }

Note that in do{...} in while(i>0), if i satisfies i>0 when while first starts, it stops as soon as i does not satisfy i>0 once in the round.

15The result of running the following program is3,1,-1,3,1,-1 

        #include<>
        int main()
       {
             int i,j;
             for(i=0;i<3;i++,i++)
             {
                   for(j=4;j>=0;j--)
                   {
                         if((j+i)%2)
                         {
                                j--;
                                printf("%d,",j);
                                continue;
                          }
                          --i;
                          j--;
                          printf("%d,",j);
                    }
              }
              return 0;
        }

Parsing:If j+i is odd, execute the statement sequence of the if structure, otherwise execute the subsequent statements of the if statement. i=0,j=4, i+j is 4, even, execute --i, j -- after i=-1, j=3, output j. j -- after j=2, execute the inner loop, j+i is 1, j = 1 after j --, output j. Execute the continue statement to end the current loop. (Parsing to be added).

16. The following program runs as   1,-2   (Just a little attention.)

       #include<>
       int main()
       {
           int y=2,a=1;
           while(y--!=-1)
              do
              {
                   a*=y;
                   a++;
               }while(y--);
           printf("%d,%d\n",a,y);
           return 0;
     }

17、The function of the following program is to output the number of numbers in the range of 1~100 whose product is greater than the sum of the numbers, please fill in the blanks to make the program complete.

       #include<>
       int main ( )
       { 
             int n,k=1,s=0,m;
             for (n=1;n<=100;n++) 
             {
                   k=1;s=0;
                   m=n;
                   while (m!=0
                   {
                        k*=m%10;
                        s+=m%10;
                        m=m/10;
                    }
                    if (k>s) 
                        printf("%d",n);
             }
             return 0;
       }

Ans: Since n is the loop control variable of the for statement and the while statement divides m by 10, the first blank should be filled with m=n. The while statement performs digit separation (dividing by 10 and then dividing by 10 until the number is 0), so the second blank should be filled with m or m>0. The third blank should be filled with m=m/10.

18. The following formula is known:

The function of the following program is to output the pai value of eps that satisfies the accuracy requirement based on the above equation, fill in the blanks to make the program complete.

         #include<>
         int main()
         {
               double s=1.0,eps,t=1.0;
               int n;
               for(n=1;t>=eps;n++)
               {
                     t=t*n/(2*n+1);
                     s+=t;
               }
               printf("%lf\n",2*s);
               return 0;
         }

19、The function of the following program segment is to calculate how many zeros are at the end of 1000!

Answer: m%5==0

Ans: In finding factorial, whenever any number is divisible by 5, a zero is added, so m%5==0 should be filled in

20, the following program accepts input from the keyboard until the Enter key is pressed, these characters are output as is, but if there is more than one consecutive space when only one space is entered, please fill in the blanks to make the program complete.

Answers: cx=getchar() cx!=front cx 

Ans: Type the characters from the keyboard till the enter key and remove the extra space characters. So the first blank should be filled with cx=getchar(). If the current character is a space, to determine whether the previous character is a space, the second blank fill cx!=front. if the previous character is not a space, indicating that there are not two consecutive spaces, the direct output cx.

21, (Programming Question) Enter a positive integer and find all its prime factors.

Ans: Starting from the smallest prime number k=2, if it can divide the input positive integer m, then use the quotient of m after it is divided by k as the new number, and continue to remove it by k. If it cannot be divided by k, then add 1 to k to continue the previous process until k is greater than or equal to m. The flowchart of the program is shown in the figure:

The program is as follows:

         #include<>
         int main()
         {
               int m,k=2;
printf("Input a positive integer: \n");
               scanf("%d",&m);
               while(k<m)
                     if(m%k==0)
                     {
                            printf("%4d",k);
                            m=m/k;
                      }
                      else
                            k++;
                 printf("%4d\n",m);
                 return 0;
           }

22. The keyboard enters the positive integers n and a. Find s = a + aa + aaa +... +aa... . a (n a)

           #include<>

           int main()

           {

                 int a,n,s=0,p=0,i;

                 scanf("%d %d",&n,&a);

                 for(i=0;i<=n;i++)

                 {

                       p=p*10+a;

                       s=s+p;

                 }

                 printf("%d\n",s);

                 return 0;

            }

23, Find the roots of the equation 2X^3-4X^2+3X-6=0 in the interval (-10, 10) by the method of bisection

         #include <>
         #include <>

         int main()
         {
                float a=-10,b=10,x,f1,f2,f;
                f1=(((2*a-4)*a+3)*a)-6;
                f2=(((2*b-4)*b+3)*b)-6;
                do
                {
                      x=(a+b)/2;
                      f=(((2*x-4)*x+3)*x)-6;
                      if(f*f1<0)
                      {
                           b=x;
                           f2=f;
                      }
                      else
                      {
                           a=x;
                           f1=f;
                      }
                }while(fabs(f)>=1e-6);
                printf("%6.2f\n",x);
                return 0;
         }

24, Write a program to compute the approximation of.

 

25. Remove all the odd digits in an unsigned decimal integer and form a new number in the original order.

Ans: Using the separation of digits (divide by 10 to get the balance, divide by 10), the separated digits will be used to determine the odd-even number, and if it is odd, it will be composed into one of the new digits. Due to the original order of the composition of the new number, that is, the first separated from the low, you can keep multiplying by 10, plus the number of previous composition to achieve. Program flow chart shown in Figure.

           #include <>
           int main()
           {
                  unsigned long s,t=0,p=1;
                  scanf("%u",&s);
                  while(s!=0)
                  {
                        if((s%10)%2!=0)
                        {
                              t=t+(s%10)*p;
                              p=p*10;
                        }
                        s=s/10;
                  }
                  printf("%u\n",t);
                  return 0;
           }