[Summer Daily Practice] day9

Table of contents

multiple choice

(1)

Parse: 

(2)

Parse:

(3)

Parse: 

(4)

Parse: 

(5)

Parse: 

programming questions

Question one

describe

example

hint 

analyze 

Code

Question two

describe

example

hint

analyze

Code

Summarize


multiple choice

(1)

1. The output of the following program is ( )

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

A: There is an error in the above program B: 6 C: 8 D: 12
Answer: D

Parse: 

p is an array of pointers, p[i] = &a[i*3] is equivalent to separating the array a into groups of 3 and storing the first address of each group in the p array. At this time, p is like a binary array with 4 rows and 3 columns. Dimensional array, p[3][2] is the third element of row 4 12

(2)

2. The two-dimensional array X is stored in row order, and each element occupies one storage unit. If the storage address of X[4][4] is Oxf8b82140, and the storage address of X[9][9] is Oxf8b8221c, then the storage address of X[7][7] is ( )

A: Oxf8b821c4 B: Oxf8b821a6 C: Oxf8b82198 D: Oxf8b821c0
Answer: A

Parse:

Suppose there are n elements in each row: the address of the x[9][9] element - the address of the x[4][4] element = 0x21c-0x140=5n+5 (21c and 140 are sixteen of the last three bits of the address base number), where n is 43, assuming that the address of x[7][7] is z, the address of x[7][7] element - the address of x[4][4] element = z-0x140 = 3n +3, z = 3n+3+140 =3*43+3+0x140 = 0x84+0x140 = 0x1c4, look at the mantissa of the address, choose A

(3)

3. Which of the following options can correctly describe sizeof(double) ( )

A: An integer expression B: A double expression C: An invalid expression D: A function call
Answer: A

Parse: 

sizeof is an operator in C language, not a function call. Simply put, its function is to return the number of memory bytes occupied by an object or type. The result is an unsigned integer, so it can be regarded as an integer expression . So choose A

(4)

4. What is the result after running the following code? ( )

int main()
{
    char a = 'a',b;
    printf("%c,", ++a);
    printf("%c\n", b = a++);
    return 0;
}

A: b,b  B: b,c  C: a,b  D: a,c
 答案:A

Parse: 

The character 'a' is stored in the variable a, the first output is added first and then output, and the output is 'b'; when the second output is made, a is first assigned and then added, and the value assigned to b is the original value of a Value, when b is output, it is still 'b'

(5)

5. The value of the following comma expression is ( )

(x= 4 * 5 , x * 5) , x + 5;

A: 25 B: 20 ​​C: 100 D: 45
 Answer: A

Parse: 

The comma expression calculates sub-expressions from front to back, and the result is the value of the last item. The expression after removing the parentheses in this question is equivalent to the original expression. First calculate 4*5 and assign it to x , x becomes 20, the middle x*5 does not change the value of x, the last x+5 value is 25, which is the value of the entire expression

programming questions

Question one

describe

A self-divisor is a number that is divisible by every digit it contains.

For example, 128 is a divisor because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
Divisors are not allowed to contain 0.

Given two integers left and right , return a list whose elements are all divisors in the range [left, right].

example

hint 

analyze 

For the judgment of self-divisor, each digit of the number can be divisible by the source number. With this rule, we only need to loop to obtain each digit of a number, and then take the modulus with the source number to determine whether it is 0. If there is any one in the middle If it is not 0, it means that it is not a divisor.

Next, you only need to traverse each element in the array to determine whether it is a self-divisor, and if so, add it to the returned array.

Code

int* selfDividingNumbers(int left, int right, int* returnSize)
{
    int *ret = (int *)calloc(1000, sizeof(int));//动态申请足够大的空间用于存放返回的自除数
    *returnSize = 0;
    for (int i = left; i <= right; i++) 
    {
        int num = i;
        while(num)
        {
            int remainder = num % 10;//计算余数
            if (remainder == 0 || (i % remainder) != 0) 
            {//判断i自身与余数取模是否为0
            break;
            } 
            num /= 10;
        } 
    //如果num==0表示通过了每一位数的取模判断,则i就是自除数
        if (num == 0) 
         ret[(*returnSize)++] = i;
    } 
return ret;
}

Question two

describe

Given an integer array nums, return an array answer where answer[i] is equal to the product of all elements in nums except nums[i].

The topic data guarantees that the product of all prefix elements and suffixes of any element in the array nums is within the range of 32-bit integers.

please don't use division

example

 

hint

 

analyze

If violence does not consider other factors, multiply all the data, and then traverse the array and divide by the current position data.
A better solution: divide the multiplication into two operations, first calculate the product of the data on the left side of each position and put it in the return array, and then loop for the second time

Calculate the product of the data on the right side of the corresponding position and multiply it with the product of the left half of the corresponding position in the returned array to obtain the result.

Example: an array int nums[] = {2, 3, 4}

Code

int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
    int *ret = (int *)malloc(numsSize * sizeof(int));
    *returnSize = numsSize;
    int left = 1, right = 1;
    //第一次循环,将当前位置左边的数字乘积填入返回数组中
    for (int i = 0; i < numsSize; i++) 
    {
        ret[i] = left;// 1 nums[0] nums[0]*nums[1] num[0]*nums[1]*nums[2] ....
        left *= nums[i];
    }
    //第二次循环,对于返回数组的元素从后往前进行,每次乘以右边元素的乘积
    for (int i = numsSize - 1; i >= 0; i--) 
    {
        ret[i] *= right; //最后一个成绩不需要乘以最后元素,乘以1就行
        right *= nums[i]; //right变化:1 nums[end] nums[end]*nums[end-1] .....
    } 
    return ret;
}

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