C language exercises Day2

Insert image description here
Today I will continue to share C language practice exercises

1. Multiple choice questions

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

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

A: 12 B: 13 C: 16 D: None
of question tests our understanding of escape characters. \ means \ and counts as one character, because strlen is to find the number of characters before \0. So we look at it one by one. \123 is a hexadecimal number. \t counts as one character. Then there are 12 characters. The answer to this question is A. 2. If there is the following program, after
running The output result 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;
}

We can write this question by reading the code. Define N as 2, define M as N+1, num=(N+1+1)*N+1/2, and the result we get after calculation is 8 3. As
follows The value of function f(1) 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
First of all, this is a simple recursive idea. It will only return when n=5. The static here will allow i to continue to grow, because when we call the function, Create a function stack frame. If it is not modified with static, it will be a local variable and will be destroyed when it is popped off the stack. Therefore, adding static will turn it into a static local variable. So let’s look at this question now. We can actually just write a draft.
Insert image description here
So the answer is 7

4. Do the following three pieces of program code have the same effect? ​​( )

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

Remember our slogan, left fixed value right directed, this problem can be solved. Let's take a look at option
A: (2)=(3) B: (1)=(2) C: None D: Both Same.
According to our slogan, the answer is 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© B: For a
certain A double variable a, you can use a == 0.0 to determine whether it is zero
C: Initialization method char a[14] = "Hello, world!"; and char a[14]; a = "Hello, world!"; The effect is the same.
D. The above statements are not correct
. Let’s look at them one by one. First of all, option A is definitely wrong. The structures need to be aligned, so they cannot be equal. We can also draw a picture and calculate it here. If you don’t understand, you can read our previous article. Article about structure memory alignment, let's calculate it.
First, our default compiler's memory alignment number is 8, and then short occupies 2 bytes and int is 4. We have to start from an integer multiple of the alignment number of int, so from Starting from 4, we go down four to 7, and then char plus one is 8, so the size of our structure is 9.

Option B is also wrong. The reason is that our data storage methods are different. I have also written a specific article about the storage method of data. If you don’t understand, you can check it out. Then there is option C. One is a string and the other is an array. The effect is sure
. Different, the space opened up is different.
So our answer is D

1. Verify Nicochers' theorem, that is: the cube of any integer m can be written as the sum of m consecutive odd numbers. For example:
input a positive integer m (m≤100), write the cube of m as the sum of m consecutive odd numbers and output it.
Note: This question contains multiple sets of input data.
Input description: Input an int integer
Output description: Output the decomposed string
OJ link [Niuke.com Question Number: HJ76 Nicoches Theorem] [Difficulty: Easy]

1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19

In fact, the key to this question is that it is difficult to calculate the first number. It is difficult to figure out the number at the beginning. If you can calculate the first number, this question is very simple. I didn't figure it out at the beginning hahahaha

#include <stdio.h>
 
int main()
{
    
    
   int m=0;
   scanf("%d",&m);
   int i=0;
   int start=0;
   start=m*(m-1)+1;
    printf("%d",start);
   for(i=1;i<m;i++)
   {
    
    
        start+=2;
        printf("+%d",start);
        
   }
   return 0;
}

Question 2
Arithmetic Sequence

Insert image description here
If you know the question above, this question will definitely be easier, so I won’t explain it.

#include<stdio.h>
int main()
{
    
    
	int n=0;
	scanf("%d",&n);
	int start=2;
	
	int i=0;
	int sum=2;
	for(i=1;i<n;i++)
	{
    
    
		start+=3;
		sum=sum+start;
		
	}
	printf("%d",sum);
	return 0;
}

That’s it for today’s daily C language sharing, see you next time

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/132531268