C language practice every day (15)

Preface:
Daily practice series, each issue contains 5 multiple choice questions and 2 programming questions . The blogger will explain it in as much detail as possible so that even beginners can understand it clearly. The daily practice series will continue to be updated, and will be updated based on academic performance during the school year.

 Five multiple choice questions:

1. The result of running the program is ()

#include<stdio.h>
int main()
{
int pad = 0; int pAd = 0;
int sum = 5;
pad = 5;
pAd = (sum++,pAd++, ++pAd);
printf("%d %d\n", pAd,pad);
}

A、1,5      B、2,5      C、1,6     D、2,6

Analysis: First define sum and pad as 5, followed by a series of comma expressions, proceeding from left to right, and the last value is the last expression. We can go a step further, sum++, let sum be 6, pAd++, let pAd= 1 (Note: No prefix or postfix is ​​needed here. You can regard the expression between comma expressions as a separate block. The computer will execute the contents of this block before executing anything else). The last step can be As pAd=(++pAd), the independent block ++pAd is executed first, let pAd be 2, and pad is always 5, so the final printed result is, 2, 5, choose B

The pre- and post-results are consistent, and the proof can be regarded as an independent block.

 

 2. The result of running the program is ()

#include <stdio.h>
int sum(int a)
{
    int c = 0;
    static int b = 3;
    c += 1;
    b += 2;
    return (a + b + c);
}
int main()
{
    int i;
    int a = 2;
    for (i = 0; i < 5; i++) 
    { 
        printf("%d ", sum(a)); 
    } 
} 

A、8 8 8 8 8 8                                                                                                                                    B、 9 11 13 15 17                                                                                                                              C、10 12 14 16 18 20                                                                                                                        D、8 10 12 14 16 18

Analysis: Observing sum, we can find that the value it returns is the sum of a+b+c, and sum does not perform additional operations on a, so every time a is 2; every time c comes in, it will be redefined as 0, and then c+ =1, so c is 1 every time. Let’s talk about b. b is a static type variable, which means that the life domain of b is global and it will only be defined once. The first time b is defined as 3, and then b+=2, let b be 5, the second time b+=2, let b be 7, the third time, b is 9, the fourth time b is 11, the fifth time b is 13, and a+c is always 3, So the final printed result is 8 10 12 14 16 18, choose D

 

3. The correct statement about the if statement is ( )
A. The if statement can only be followed by one statement.
B. In the if statement, 0 means false, 1 means true, and the other meanings are meaningless.
C. The if statement is a branch statement that can implement a single branch. , you can also implement multi-branch
D. Else statements always match their corresponding if statements. 

Analysis: Option A is wrong. There can be many statements after if, just {} is enough. Option B is wrong and too one-sided. All non-0 statements in C language are true. Option C is correct, else if means multiple branches. Option D is wrong. Without parentheses, it defaults to the nearest match.

4. The result of running the program is ()

#include<stdio.h>
int func(int a)
{
    int b;
    switch (a)
    {
        case 1: b = 30;
        case 2: b = 20;
        case 3: b = 16;
        default: b = 0;
    }
    return b;
}
int main()
{
  int x=3
  printf("%d",func(x));
}


A、 30    B、20    C、16    D、0

Analysis: If there is no break in the switch, it will continue along. This question is like this. There is no break in its case statement, then it will continue along until the final b=0, so The final returned value is also 0, choose D.

 5. The result of running the program is ()

#include <stdio.h>
int main()
{
   int a = 0, b = 0;
    // for循环将a和b的初始值均设置为1
   for (a = 1, b = 1; a <= 100; a++)
   {
   		if (b >= 20) break;

   		if (b % 3 == 1)
   		{
			b = b + 3;
			continue;
   		}

  		 b = b-5;
   }
   printf("%d\n", a);
   return 0;
}

A. 7 B. 8 C. 22 D. Infinite loop 

Analysis: Observing the code, you can find that the main part is a loop. The goal is to print out the value of a, and the value of a will only be ++ at the end of the loop. It is not difficult to see that the number of complete loops +1 (why +1 , because a first ++ before breaking out of the loop) is the value of a, b>=20 will jump out of the loop, and b will increase by 3 if %3==1, then b will keep increasing by 3 until >= 20. It can be seen that if 3 is added 7 times, b will be >= 20, so the complete cycle is performed 7 times, so in the end a is 8, choose B 

 

Programming question 1:

Word Analysis - Lanqiao Cloud Class (lanqiao.cn)

Idea: You only need to judge lowercase letters and there are 26 letters in total, so create an integer array with a length of 26 to store the number of occurrences of each letter, and then compare to get the maximum. One thing to note is to remember to store the corresponding subscript 

int main(int argc, char *argv[])
//这个参数不用看它,你就把它当作没有即可
{
int count[26]={0};
//分别储存26个字母的出现次数
char ch=0;
while((ch=getchar())!='\n')
{
  count[ch-'a']++;
  //对应字母-'a'可以得到对应的下标
}
int i=0;int max=0;int max_i=0;
for(i=0;i<26;i++)
{
   if(count[i]>max)
   {
     max=count[i];//储存最多出现次数
     max_i=i;//下标也储存
   }
}
printf("%c\n%d",max_i+'a',max);
}

Programming question 2: 

House plate production - Lanqiao Cloud Class (lanqiao.cn)

 

Tip: The %10 operation can remove the last digit of the number, and the /10 operation can delete the last digit of the number. 

#include <stdio.h>
int main()
{
  int i=0;int count=0;
  for(i=1;i<=2020;i++)
  {
    int number=i;
    while(number)
    {
      if(number%10==2)
      {
        count++;
      }
      number/=10;
    }
  }
  printf("%d",count);
  return 0;
}

Guess you like

Origin blog.csdn.net/fq157856469/article/details/132870912