C language daily practice (8)

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.

 Five multiple choice questions:

1. Please read the following program, the running result is ( )
 

#include<stdio.h>
int main()
{
char c='A';
if('0'<=c<='9') printf("YES");//1
else printf("NO");
return 0;
} 

A, YES B, NO C, YES D, statement error

Analysis: Error-prone questions. Many people will subconsciously think that when they see code 1, its judgment condition is that the variable c is greater than or equal to character 0 and less than or equal to character 9 before printing YES. In fact, to realize such a function code, it must be '0'<=c&&c<='9'

The execution of code 1 is to perform the operation '0'<=c first, and then compare the value obtained by the operation with '9', c stores the character variable 'A', the ASCII code value is 65, '0' The ASCII code value is 48. Therefore, the value obtained by '0'<=c is 1, and then compared with '9', obviously 1<='9', the final expression is true, and the print of YES is executed. Therefore choose C

2. What is the output of the following program ( )

#include<stdio.h>
int main()
{
int n = 1001;
int ans = 0;
for(int i = 1; i <= n; ++i)
{
ans ^= i % 3;//1
}
printf("%d",ans);
return 0;
}

A、-2         B、0        C 、1        D、2

Analysis: The calculation rule of the ^ (exclusive OR) operator is to change the same bit of two numbers to 0 and the different bit to 1, then two identical numbers ^ equal to 0, and the ^ operator also satisfies the commutative law, so When performing ^, the positions between numbers can be exchanged arbitrarily.

Observing the code, it is found that the goal is to print out the value of ans. Ans is assigned a value of 0 at the very beginning, and then enters the for loop, goes to code 1, and keeps ^i%3 the value. i%3 can see that it will cycle among the three numbers of 0, 1, and 2, and the beginning of the cycle is 1, so the cycle is 1, 2, 0, then ans is constantly ^1, 2, 0. Observation shows that the number of cycles is 1001, 1001/3=333 remainder 2. And the 332nd time 1, 2, 0 is an even number, which means that at the 332nd time, 1, 2, 0 are all one-to-one pairs, then all will be ^ to 0, so the final result is (0^ 1^2)^1^2, equal to 0, so choose B

3. The following statement about null pointers and uninitialized pointers is wrong ( )
A. It is illegal to take the value of the address 0x0
B. The null pointer can ensure that it does not point to any object or function; while the uninitialized pointer may point to any place
C. A null pointer is not equal to the pointer value of any object or function D. malloc returns an uninitialized pointer when its memory allocation fails

Analysis: A null pointer cannot be dereferenced, and A is correct. A null pointer is a pointer that does not point to any space. An uninitialized pointer is a wild pointer, which is unsafe and may point anywhere. B is correct. A null pointer does not point to any space and has no value, so C is correct. If malloc fails to create, it will return a null pointer, so D is wrong. choose D

4. There is the following function, the function of which is ( )

int fun(char *s)
{
char *t = s;
while(*t++);//1
return(t-s);//2
}

A. Compare the size of two characters B. Calculate the number of memory bytes occupied by the string pointed to by s
C. Calculate the length of the string pointed to by s D. Copy the string pointed to by s to string t

Analysis: error-prone questions, the best way to encounter this kind of question is to pass something in and try it out, we pass the string "abc" in, it can be seen that the function fun first uses the character pointer t to store the pointer of s, so this When t can be equivalent to the first address of "abc", it is a loop to go to code 1, and the loop will make t go continuously until it reaches '\0', because the ASCII code value of '\0' is 0. And the string "abc" walks three times before reaching '\0'. But pay attention, *t++, that is, it will go to a position after '\0'. Finally, go to code 2, subtract the first address from the next address, and you will get the length of the string "abc" + 1 = 4. To sum up, the answer is B, and the calculation is the number of memory bytes occupied by the string, including '\0'

 5. If there is float a[3]={1.5,2.5,3.5},*pa=a;*(pa++)*=3; then the value of *pa is ()

A、1.5      B、2.5      C、3.5      D、4.5

Analysis: Looking step by step from left to right, first a floating-point array a is defined, storing 1.5, 2.5, 3.5. Next, use a floating-point pointer to assign the first address of the floating-point array a to pa, then the value stored in pa is 1.5, and finally *(pa++)*=3, use first and then ++, dereference according to priority 1.5*=3 is obtained, then the value pointed to by the first address of array a is modified to 4.5. Then pa++, come to the address where 2.5 is located, *pa dereferences, so the final value is 2.5, choose B


  

Programming question 1: 

 Count the total number of rabbits every month

Idea: Through some calculations, you can find that the total number of rabbits in the nth month is the nth Fibonacci number, and the rest is very simple.

#include <stdio.h>
int main()
{
    int a=1;int b=1;
    int month=0;
    scanf("%d",&month);
    if(month<3)
    //当要计算的斐波那契数<3直接打印1
    {
        printf("%d",a);
        return 0;
    }
    while(month-2)
    {
        int tmp=a;
        a=b;
        b=tmp+b;
        month--;
    }
    printf("%d",b);
}

 Programming question 2:

Greatest Common Divisor__nowcoder.com

Idea: Use the rolling and dividing method to solve the problem. The specific method is: divide the larger number by the smaller number, then use the remainder (the first remainder) to divide the divisor, and then use the remainder (the second remainder) to divide the second. A remainder, and so on, until the final remainder is 0. If you are looking for the greatest common divisor of two numbers, then the final divisor is the greatest common divisor of the two numbers. 

#include <stdio.h>
int main() {
	long long int a = 0; long long int b = 0;
    //使用long long,防止数据过大
	scanf("%lld %lld", &a, &b);
	while (b)
	{
		long long int tmp = b;
        //储存中间变量
		b = a % b;
		a = tmp;
	}
	printf("%lld", a);
	return 0;
}

  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/132311372