[Summer Daily Practice] day6

Table of contents

multiple choice

(1)

analyze 

(2)

analyze 

(3)

analyze

(4)

analyze

(5)

analyze

programming questions

Question one

describe

example

hint 

analyze 

Code

Question two

describe

example

hint 

analyze 

Code

Summarize


multiple choice

(1)

1. Which of the following statements is correct ( )

A: The break statement can only be used in the loop body and the switch statement body
B: When break appears in the switch statement body in the loop body, its function is to jump out of the switch statement body and stop the execution of the loop body C
: The function of the continue statement Yes: Stop the loop after executing the remaining statements in the loop body
D: The continue statement cannot be used in the while statement and do-while statement
Answer: A

analyze 

The break statement is usually used in loop statements and switch statements. When break is used in the switch statement, the program can jump out of the switch and execute the statement after the switch; when the break statement is used in the do-while, for, while loop statement, it can make the program terminate the loop and execute the statement after the loop, That is, when the condition is met, it will jump out of the loop. The function of the continue statement is to skip the remaining statements in the loop body and forcibly execute the next loop. There are errors in options B, C and D. So option A is correct

(2)

2. The number of the following for loops is ( )

for(int i = 0 ; i || i++ < 5;);

A: 0 B: 5 C: 1 D: infinite
Answer:  D

analyze 

Logical or operation: if the previous expression is true and the latter expression is not calculated, i is 0 in the first loop, and i++ is executed, and i is 1 in the second loop, which is a true value, and i++ is no longer executed, and there is an infinite loop up

(3)

3. The correct one in the following description is ( )

A: Since the loop body statement in the do-while loop can only be an executable statement, compound statements cannot be used in the loop body. B: The
do-while loop starts with do and ends with while, and you cannot write subtitles after the while (expression) No.
C: In the body of the do-while loop, there is not necessarily an operation that can make the value of the expression behind the while become zero ("false") D: In the do-
while loop, the while can be omitted according to the situation
Answer: C

analyze

The loop body in the do-while loop is usually a compound statement code block, A is wrong, a semicolon must be written after the while (expression), B is wrong, while cannot be saved, and D is wrong

(4)

4. Suppose the description of the function fun and the actual parameter group is as follows, then in the calling statement of the function, the correct one is ( )

void fun(char ch,float x[]);
float a[10];

A: fun("asd" , a[]); B: fun('x' , A); C: fun('68' , 2.8); D: fun(32 , a)
答案: D

analyze

For option A, you only need to write the array name to pass the array parameter, a[] is wrong, and the second parameter of option B is written in uppercase, which is wrong. The second parameter of the C option is a floating point number, but the second parameter of the fun function is an array that does not match. The fun function parameter x needs to pass an array or float * pointer, and only the form of the D option is correct.

(5)

5. In C language, if a function does not write the return value type, the default return type is ( )

A: int B: char C: void D: None
Answer: A

analyze

A function does not write the return value type, the default return type is int, but this is not recommended
 

programming questions

Question one

describe

You are given an array of integers nums in which there is always only one largest integer.

Please find the largest element in the array and check if it is at least twice every other number in the array. If yes, returns the subscript of the largest element, otherwise returns -1 

example

hint 

analyze 

Brute force cracking : double loop through the array, and judge whether each element is twice the size of other elements. Or go through it first to find the maximum value, and then go through it to determine whether it is twice the other number.
Better thinking : Find the largest number and the second largest number in one traversal, and judge whether the largest number is twice the second largest number.

Code

int dominantIndex(int* nums, int numsSize) {
	if (numsSize == 1) return 0;//特殊情况只有一个元素则特殊处理
	int max, sec, idx;
	//先对最大和次大进行选择赋值,注意max和sec不能随意赋初值,因为有可能你赋予的初值就是最大值
	//因此要使用数组中的数据进行初值赋予。
	if (nums[0] > nums[1]) {
		max = nums[0];
		idx = 0;
		sec = nums[1];
	}
	else {
		max = nums[1];
		idx = 1;
		sec = nums[0];
	} 
	for (int i = 2; i < numsSize; i++) {
		if (nums[i] > max) { //当前元素大于max,则意味着要替换
			sec = max; //先将原先最大的保存到sec中,则他就是次大的
			max = nums[i]; //再将最大的放到max中
			idx = i; //保存max值的下标
		}
		else if (nums[i] > sec) {
			//避免刚好nums[0]就是最大的情况,因为不切换最大而导致次大无法判断情况
			sec = nums[i];
		}
	} if
		(max >= sec * 2) {
		return idx;
	} 
	return - 1;
}

Question two

describe

Given two arrays  nums1 and sum  nums2 , return  their intersection  . Each element in the output must be unique. We can ignore the order of the output results.

example

hint 

analyze 

Brute force cracking is enough. Each number in the nums1 array is judged whether it exists in the nums2 array. In this way, the intersection data is found. After finding out, it is judged whether the array already exists in the returned array. If it does not exist, add it to Just return it in the array

Code

int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
	static int arr[1000];
		* returnSize = 0;
	int i, j, k;
	for (i = 0; i < nums1Size; i++) {
		for (j = 0; j < nums2Size; j++) {
			if (nums2[j] == nums1[i]) break;//判断nums1[i] 是否在nums2数组中
		} if
			(j == nums2Size) {// nums1中i位置的数据在nums2数组中不存在,则非交集数据
			continue;
		} 
		//只有在另一个数组中存在的数据才能走下来,判断是否已经被添加到返回数组中
			for (j = 0; j < *returnSize; j++) {
				if (nums1[i] == arr[j]) break;//判断nums1[i] 是否在 arr 这个返回数组中
			} if
				(j == *returnSize) {//不在返回数组中,则添加到返回数组中
				arr[*returnSize] = nums1[i];
				*returnSize += 1;
			}
	} 
	return arr;
}

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