[Summer Daily Practice] day14

Table of contents

multiple choice

(1)

Parse: 

(2)

Parse: 

(3)

Parse: 

(4)

Parse: 

(5)

Parse: 

programming questions

Question one

describe

example

 hint

Parse: 

Code

Question two

describe

example

Parse: 

Code

Summarize


multiple choice

(1)

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

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

A: Compare the size of two characters B: Calculate the number of bytes of memory 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
Answer: B

Parse: 

The loop stops when *t is 0, and at the same time t++, t will finally stop at a position after the '\0' at the end of the string, t is used as the tail pointer minus the head pointer is the number of bytes of memory occupied by the entire string, Including \0; and the c answer string length does not include the last \0

(2)

2. 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
Answer: B

Parse: 

In *pa=a, the pointer pa points to a[0]; the return value of pa++ is still the value before the operation; *(pa++) takes the value of the address pointed to by pa; *(pa++)*=3 changes the value to the original 3 times, that is, the first value of the array a is 4.5; since the pa pointer moves sizeof(float) bytes after pa++, pa points to a[1], so the value is 2.5
 

(3)

3. The output of the following program after running is ( )
 

#include <stdio.h>
int main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, *p = a + 5, *q = NULL;
    *q = *(p+5);
    printf("%d %d\n", *p, *q);
    return 0;
}

A: Error reported after running B: 6 6 C: 6 11 D: 5 10
 Answer: A

Parse: 

It is wrong to initialize the pointer q to NULL and then dereference the pointer q. The NULL pointer cannot be dereferenced.
 

(4)

4. If there is a definition of char *p[]={"Shanghai","Beijing","Honkong"}; then the result is the expression of character j ( )

A: *p[1] +3  B: *(p[1] +3)  C: *(p[3] +1)  D: p[3][1]
答案:B

Parse: 

Option B, p is an array of char* type, p[1] gets the first address of the string "beijing", add 3 to get the address of 'j', solve the address to get 'j'
 

(5)

5. Which of the following statements is correct ( )

A: Even without mandatory type conversion, the base type of the pointer variable can be different when performing pointer assignment operation
B: If you try to access a storage unit through a null pointer, you will get an error message
C: Let the variable p be A pointer variable, the statement p=0; is illegal, p=NULL should be used;
D: Relational operators cannot be used for comparison between pointer variables
Answer: B

Parse: 

The description of option A is incorrect, pointers of different types generally cannot be assigned directly; in option C, p=NULL; and p=0; are equivalent; in option D, two pointer variables pointing to the same array can be represented by relational operations The positional relationship between the indicated array elements. option B is correct

programming questions

Question one

describe

The game of master mind is played as follows.

The computer has 4 slots, and each slot holds a ball, which may be red (R), yellow (Y), green (G), or blue (B). For example, a computer might have RGGB 4 (red for slot 1, green for slots 2, 3, blue for slot 4). As a user, you are trying to guess the color combination. For example, you might guess YRGB. If you guess the color of a certain slot correctly, it counts as a "guess right"; if you guess only the color but the slot is wrong, it counts as a "false guess". Note that "right guess" cannot be counted as "false guess".

Given a color combination solutionand a guess guess, write a method that returns the number of correct and false guesses answer, where answer[0]is the number of correct guesses and answer[1]the number of false correct guesses.

example

 hint

Parse: 

Traverse the two arrays to count the number of correct guesses and the number of false guesses.
Number of guesses: If the position is the same and the color characters are the same, the number of guesses counter + 1
The number of false guesses: the color is the same, but in different positions, at this time only need In addition to guessing the position, count the number of characters in the two arrays, and the smaller one is the number of false guesses for each color

Code

int* masterMind(char* solution, char* guess, int* returnSize) {
	*returnSize = 2;
	static int arr[2] = { 0 };
	arr[0] = 0; arr[1] = 0;//静态空间不会进行二次初始化因此每次重新初始化,可以使用memset函数
	int s_arr[26] = { 0 };//26个字符位 solution 四种颜色数量统计
	int g_arr[26] = { 0 };//26个字符位 guess 四种颜色数量统计
	for (int i = 0; i < 4; i++) {
		if (solution[i] == guess[i]) {
			arr[0] += 1;//位置和颜色完全一致则猜中数量+1
		}
		else {
			//统计同一位置不同颜色的两组颜色数量,伪猜中不需要对应位置相同,只需要有对应数量的颜色就行
			s_arr[solution[i] - 'A'] += 1; //统计solution对应颜色字符出现次数
			g_arr[guess[i] - 'A'] += 1;//统计guess对应颜色字符出现次数
		}
	} 
	//在两个颜色数量统计数组中查看颜色数量,取相同位置较小的一方就是为猜中数量
		for (int i = 0; i < 26; i++) {
			arr[1] += s_arr[i] > g_arr[i] ? g_arr[i] : s_arr[i];
		} 
		return arr;
}

Question two

describe

Given an integer array numbers and a target value target, please find the subscripts of two numbers that add up to the target value in the array, and the returned subscripts are arranged in ascending order.

(Note: The subscript of the returned array starts from 1, and it is guaranteed that the target can be obtained by adding the two numbers in the array)

example

Parse: 

After getting a number num in the array, check whether there is a number equal to target - num among the remaining numbers.

Code

int* twoSum(int* numbers, int numbersLen, int target, int* returnSize) {
	*returnSize = 2;
	static ret_arr[2] = { 0 };
	memset(ret_arr, 0x00, sizeof(ret_arr));//静态空间不会二次初始化,因此手动初始化
	for (int i = 0; i < numbersLen; i++) {//从第0个位置开始一个一个数字找
		for (int j = i + 1; j < numbersLen; j++) {//从第一个数字往后的数字中找出另一个数字
		//与numbers[i]相加等于target的数字找到了则i和j就是对应两个数字下标
			if (numbers[i] + numbers[j] == target) {
				ret_arr[0] = i + 1;//题目要求下标从1开始
				ret_arr[1] = j + 1;
				return ret_arr;
			}
		}
	} 
	*returnSize = 0;//没有符合的下标则返回数组大小为0;
	return NULL;
}

Summarize

This is the end of the explanation of today's practice. Welcome to leave a message, exchange comments and make corrections. If the article is helpful to you or you think the author's writing is not bad, you can click to follow, like, and bookmark to support.

Guess you like

Origin blog.csdn.net/m0_71731682/article/details/132110524