C language daily practice (2)

Foreword:
Daily practice series, each issue contains 5 multiple-choice questions and 2 programming questions. The blogger will explain in as much detail as possible, so that beginners can listen clearly. The daily practice series will continue to be updated, and there must be an update within three days during the summer vacation. After the school starts, it will be updated according to the academic situation.

5 multiple choice questions: 

1. The output of the following program segment is ()

#include<stdio.h>
#include<string.h>
int main()
{
char s[] = "\\123456\123456\t";
printf("%d\n", strlen(s));
return 0;
}

A.12 B.13 C.16 D. None of the above is correct

Analysis: According to the analysis of the topic, the function of this string of codes is to calculate the length of the string s.    The characteristic of the strlen function is that it will stop accessing when \0 is encountered, and there will be \0 at the end of the string, although you can not see. The blogger about strlen also wrote a blog introduction before, friends who don't understand the strlen function can go and have a look. Teach you how to use common string functions (including simulation implementation) - Tomato Blog in the Sea - CSDN Blog

Since it is to calculate the length of the string, what we have to do is to count how long the string s is     . It is an ordinary character, therefore, these two \\ add together is 1 character 123456, there are 6 characters, 6+1 is 7 characters.

Going on, I met \ again. The number behind \ means that the following number is an octal number, and the maximum number is three digits.     Therefore, \123 is regarded as a whole, as a character, and 7+1 now has 8 Characters, 456 three characters, 8+3 has 11 characters, the last \t is regarded as one character, so there are 12 characters at the end, the answer is A

Many friends will forget the specific content of escape characters. Here, the blogger will give you the content that has been sorted out before.

 

2. If there is the following program, the output after running is ( )

#include<stdio.h>
#define N 2
#define M N + 1
#define NUM (M + 1) * M / 2
int main()
{
	printf("%d\n", NUM);
	return 0;
}

A.4   B.8   C.9    D.6

 Analysis: The macro definition is to replace the corresponding code with the corresponding macro before performing the operation. This string of code wants to print NUM, and NUM has a macro definition, so replace NUM with the corresponding macro before the operation, the replacement result is (M+1)*M/2, M has a macro definition, continue to replace, replace the result It is (N+1+1)*N+1/2. Note that it is just a replacement. Don’t add extra brackets. According to the macro definition, you can replace it as it is defined. N has a macro definition, the replacement result is (2+1+1)*2+1/2, and the calculation result is 8.5, but printf is going to print integer data, which will cause data loss and print out 8.

 

 3. The value of f(1) of the following function is ( )

int f(int n)
{
static int i = 1;
if(n >= 5)
return n;
n = n + i;
i++;
return f(n);
}

 A.5    B.6    C.7    D.8

 Analysis: According to the meaning of the question, our goal is f(1), and the return value of f when passing 1 when calling the int f(int n) function is our answer. Enter the function and regard n as 1. First, the function defines a static variable i and initializes it to 1. The meaning of the static variable is that it will not be destroyed with the end of the function call. It will only be destroyed after the end of the program. Similarly, it It will only be created once, that is to say, static int i=1 will only go once     if(n>=5), which means that it will be triggered when n>=5, skip first, n=n+i, then at this time n=2, keep going, i++, i is 2, return to f(2), continue to enter the f() function, but this time pass 2, i is also 2, then n will be 4 in the end, i will be 3, Continue recursion, n is still not >=5, n=n+i makes n 7, i++ makes i 4, continue recursion, this time n is 7, n>=5, abort the function, return 7. So the answer is C

 4. Are the following three program codes the same effect? ​​( )

int b;
(1)const int *a = &b;
(2)int const *a = &b;
(3)int *const a = &b;

A.(2)=(3) B.(1)=(2) C. are different D. are all the same

Analysis: The effect of const modifying pointer variables is determined according to the relative position between const and *.     Const placed on the left of * means that the corresponding pointer cannot be modified, and placed on the right of * means that the content pointed to by the pointer cannot be modified. modified. Then to observe the topic, we only need to find the same relative position. It is not difficult to find that the relative positions of (1) and (2) const and * are const on the right side of *, so the effect is the same, and the const of (3) puts On the right side of the hair, the effect is different. Therefore choose B

5. For the following statement, the correct one is ( )
A. For struct X{short s;int i;char c;}, sizeof(X) is equal to sizeof(s) + sizeof(i) + sizeof(c)
B. For a double variable a, you can use a == 0.0 to judge whether it is zero
C. Initialization methods char a[14] = "Hello, world!"; and char a[14]; a = "Hello, world! "; has the same effect
D. None of the above statements are correct

Analysis: The size of the structure needs to be achieved through alignment . The size of X in option A is calculated in this way. The short type occupies two bytes, the positions with offsets 0 and 1 are occupied, and the int type occupies 4 bytes. Byte, so it has to be aligned to a position that is a multiple of 4. Therefore, offsets 4, 5, 6, and 7 are occupied, and the space at offsets 1, 2, and 3 is wasted. Char type Occupies 1 byte, it does not need to be aligned, so the position with offset 8 is occupied, the size is 9, and then according to the rules of memory alignment, it must be aligned to a multiple of the maximum alignment, which is a multiple of 4, 10, 11 Wasted, the final structure calculation result is 12 and 2+4+1=8 are different.

Friends who do not understand the content of structure alignment can read the detailed explanation of the custom type written by the blogger before, which contains a detailed description of this issue

Detailed explanation of custom types - Programmer Sought

 Option B is to examine the comparison of the double type. Due to the error of the floating point number, it is impossible to directly judge whether the two numbers are equal. Usually, it is used to compare whether the absolute value of the difference between the two numbers is less than a small number (specifically, you can set such a value yourself. number, as an error) to determine equality. Option C, a is the first address of the array, which is a constant and cannot be changed, so A, B, and C are all wrong, so the answer is D

Programming question 1: 

P5717 [Deep Foundation 3. Learning 8] Triangle Classification - Luogu | New Ecology of Computer Science Education (luogu.com.cn)

 The only thing to pay attention to is the judgment of acute and obtuse angles, because many people forget their judgment methods

If the sum of the squares of the two shorter sides is greater than the square of the longest side, the triangle is an acute triangle

If the sum of the squares of the two shorter sides is less than the square of the longest side, the triangle is an obtuse triangle

#include<stdio.h>
int main()
{
    int a=0;int b=0;int c=0;
    //代表着三角形的三边
    scanf("%d %d %d",&a,&b,&c);
    int max=a>b?a:b;
    max=max>c?max:c;
    //计算出三边的最大边
    int min=a>b?b:a;
    min=c>min?min:c;
    //计算出三边的最小边 
    int sum=a+b+c;
    //三边总和
    a=max;
    //令a为最大边
    b=sum-min-max;
    //令b为中间边
    c=min;
    //令c为最小边
    if(b+c<=a)
    //三角形成立条件,两边之和大于第三边,不满足肯定不是三角形
    //拿较小的两条边来比较,如果较小的两边之和都能大于第三边就一定是三角形
    {
        printf("Not triangle");
    }
    else//是三角形才能进行后面的判断
    {
        if(b*b+c*c==a*a)
        //勾股定理判直角
        {
            printf("Right triangle\n");
        }
        else if(b*b+c*c>a*a)
        {
            printf("Acute triangle\n");
        }
        else if(b*b+c*c<a*a)
        {
            printf("Obtuse triangle\n");
        }
        if(a==b||b==c||c==a)
        //等腰三角形判断
        {
            printf("Isosceles triangle\n");
            if(a==b&&b==c)
            //先是等腰才是等边
           {
            printf("Equilateral triangle\n");
           }
        }
    }
}

Programming question 2:

Arithmetic Sequence_Niuke Questions_Niuke.com

According to the formula of the arithmetic sequence, add the first and the last * the number of items/2 is the answer, then calculate the first and the last 

#include <stdio.h>
int main() {
int head=0;
int end=0;
int n=0;
scanf("%d",&n);
head=2;
end=2+(n-1)*3;
printf("%d",(head+end)*n/2);
}

Well, today's practice is over here, thank you friends for visiting, I wish you all a bright future O(∩_∩)O

Guess you like

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