C language exercises

Insert image description here

Welcome to my: world

I hope the author's article will be helpful to you. Please correct me if there are any deficiencies. Let's learn and communicate together!


Preface


Fill in the blanks:

first question

Insert image description here

Question solution idea:
This question examines our grasp of operator priority + understanding of two-dimensional arrays;
Baidu: Detailed explanation of operator priority ; in the expression X[i][j] , the priority of the subscript reference operator is The highest; so if you disassemble it first, you can better understand its structure:
Insert image description here
Next, look at the options in turn: Option A: *(X[ i ]+j);
The subscript reference operator "[ ]" is better than the bracket "( )" has a higher priority . Since it is a two-dimensional array, it can be regarded as a collection of one-dimensional arrays; and this option is equivalent to X[i][j]; option B: *
X(i) [ j ]; You must know that "[ ]" precedes " * ", so the subscript reference operator operates first. This is a wrong expression; option C: *(X+i+j)
; This option can be understood as X[i+j] which is completely inconsistent with X[i][j];
option D: ( (X+i)+j); this option can be understood to be consistent with ;

second question

When the context and header files are normal, the output of the following code is ( )Insert image description here

Problem-solving ideas:
This question seems relatively simple, but if you don’t pay attention to a small detail, you may lose everything; it is a function recursion problem ;
the recursive output is "Geneius", and the output is "suieneG", but special attention is needed here: The first recursive input is ++s, skipping "G", so there is no printing of G;
Insert image description here
so the final output is: suiene

third question

The number of times of the following for loop is ( )

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

Problem-solving ideas:
This question examines the logical "or", logical "or", if there is true, it is true, if both are false, it is false; just entering the loop, i=0; entering the judgment condition, i is 0. is false, then see i++<5, this expression is calculated, if this is true, then enter the loop, when i is already ++ in the second loop, then i=1, this means that if it is true, it will not Entering the i++<5 expression; i will not change, so the expression will fall into an infinite loop;

The number of times should be: unlimited;

Question 4

Insert image description here

Option A does not consider memory alignment. Option B, examine the comparison of double types. Due to the error in floating point numbers, it cannot be directly judged whether the two numbers are equal. Usually, the absolute value of the difference between the two numbers is compared to whether it is less than a very small number (specifically, you can set such a number yourself. number, as the error) to determine equality. Option C, a is the first address of the array, which is a constant and cannot be changed, so A, B, and C are all wrong. Choose D.


Programming questions:

first question:


Address: oj address


Insert image description here

Problem-solving ideas: Sorting + double pointers
First sort the two arrays, and then use two pointers to traverse the two arrays. Then create an array arr that stores intersecting elements; it is foreseeable that the elements added to the arr array must be increasing. In order to ensure the uniqueness of the added elements, we need to additionally record the variable prve to represent the last element added to the answer array.
Initially, the two pointers point to the heads of the two arrays respectively.
Then each time the two pointers point to the elements in the array, if they are not equal, the smaller pointer moves one bit to the right; if they are equal, it is judged to be not equal to prve, otherwise it is stored in the intersecting array arr. , and update the value of prve, changing the value of prve to the value of the element that just intersected; Then both pointers move one bit to the right at the same time. When at least one pointer exceeds the range of the array, the traversal ends.

code:

int cmp (const void *e1,const void*e2)
{
    
    
    return *(int*)e1-*(int*)e2;
}

int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    
    
    //首先进行排序
    qsort(nums1,nums1Size,sizeof(int),cmp );
    qsort(nums2,nums2Size,sizeof(int),cmp );
    //创造一个数组存放相交的数组
    int *arr=(int*)malloc(sizeof(int) * (nums1Size + nums2Size));
    //设置两个指针
    int sur1=0;
    int sur2=0;
    int prve=-1;//记录上一个的相交值,判断是否要存入
    int j=0;
    while(sur1<nums1Size && sur2<nums2Size)
    {
    
    
        int num1=nums1[sur1],num2=nums2[sur2];
        if(num1==num2)
        {
    
    
            if(prve!=num1)//判断出不是相同的相交值
            {
    
    
                arr[j]=num1;
                j++;
                prve=num1;
                sur1++;
                sur2++;
            }
            else
            {
    
    
                sur1++;
                sur2++;
            }
        }
        else if(num1<num2)
        {
    
    
                sur1++;
        }
        else
        {
    
    
            sur2++;
        }
    }
    *returnSize=j;//相交数组个数
    return arr;
}

Second question:


Address: oj address


Insert image description here

Problem-solving idea:
first calculate the sum of all the elements in the array and record it to total; when traversing to i, the sum of the elements on the left is sum, then the sum of the elements on the right is total-nums[i]-sum; when the left and right When the sum of elements is equal, there is an equation: 2*sum + nums[i] =total;
Insert image description here

code:

int pivotIndex(int* nums, int numsSize){
    
    
    int total=0;
    for(int i=0;i<numsSize;i++)
    {
    
    
        total+=nums[i];
    }
    int sum=0;
    for(int i=0;i<numsSize;i++)
    {
    
    
        if(2*sum+nums[i]==total)
        {
    
    
            return i;
        }
        sum+=nums[i];
    }
    return -1;
}

Summarize

Knowledge is endless, and the journey of exploration is boring. Let me share the fun:
Lu Xun once said: Break your legs, give you a pair of crutches, and then tell you that you can't even walk without him, so learn to be grateful.


At the end: thanks for the support

What I also want to tell you is:
---------- Go all out for the process and be indifferent to the results. This
is also what I say to myself.

Guess you like

Origin blog.csdn.net/m0_66780695/article/details/132542048