Exercises to practice C language (the third part of summer)

Insert image description here


Preface

Say important things three times!
study! study! study!


1. Storage address

The two-dimensional array X is stored in row order, with each element occupying 1 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
D: Oxf8b821c0

Question analysis:
Assume that each line has n elements: then the address of the x[9][9] element - the address of the x[4][4] element = 0x21c-0x140=5n+5 (21c and 140 are the last three digits of the address hexadecimal number), where n is 43, assuming the address of x[7][7] is z, the address of the x[7][7] element - the address of the x[4][4] element = 0x140 = 3n+3, z = 3n+3+140 =3*43+3+0x140 = 0x84+0x140 = 0x1c4, look at the mantissa of the address, select A

Question answer:
A


2. Comma expression

The value of the following comma expression is ( )

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

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

Question analysis:
The comma expression calculates the 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 the value . Given x, x becomes 20. The middle x 5 does not change the value of x. The value of the last item x+5 is 25, which is the value of the entire expression.

Question answer:
A


3. The product of arrays other than itself

Question link: OJ link
Insert image description here

Tip:
2 <= nums.length <= 105
-30 <= nums[i] <= 30
ensures that the product of all prefix elements and suffix of any element in array nums is within the range of 32-bit integers

Problem analysis:
Divide the multiplication into two times. In the first time, calculate the product of the data on the left side of each position and put it into the return array. Then, in the second loop,
calculate the product of the data on the right side of the corresponding position and return it to the corresponding position in the array. Multiply the products of the left half of to get the result.

Example: an array int nums[] = {2, 3, 4}.
int left = 1, right = 1;
Calculate the left product:
the left product of the 0th element, arr[0] = left and then calculate the left product of the 1st digit
left*=nums[0] -> left = 1 2nd
The left product of 1 element, arr[1] = left and then calculate the left product of the second digit
left
=nums[1] -> left = 1 2 3
The left product of the second element, arr[2] = left and then calculate The product on the left side of the third digit is no longer necessary, because the second element is the last element.
After a loop is completed, each element in the returned array stores the product of its left element. Values ​​in arr[]: [1, 2, 6]
Calculate the right product:
the right product of the 2nd element, arr[2] = right and then calculate the 1st right product right =nums[2] -> right =1 4
The right product of the 1st element, arr[1] = right and then calculate the right product of the 0th element right =nums[1] -> right =1
43.
The right product of the 0th element, arr[0] = right. Then calculate the right product of the -1st digit -1 digit. There is no need to calculate it.
After the loop is completed, each element in the returned array is the product of other elements. arr[2]
=1; arr[1]
=4; arr[0]*=12

Question Answer:

int* productExceptSelf(int* nums, int numsSize, int* returnSize){
    
    
    *returnSize=numsSize;
    static int answer[100000]={
    
    0};
    int left=1,right=1;
    for(int i=0;i<numsSize;i++){
    
    
        answer[i]=left;
        left*=nums[i];
    }
    for(int i=numsSize-1;i>=0;i--){
    
    
        answer[i]*=right;
        right*=nums[i];
    }
    return answer;
}

4. Bytes and Binary

Find the function return value, pass in -1, then the function returns ( ) on a 64-bit machine

int func(int x)
{
    
    
    int count = 0;
    while (x)
    {
    
    
        count++;
        x = x&(x - 1);//与运算
    } 
    return count;
}

A: Infinite loop B: 64 C: 32 D: 16

Question analysis:
Executing the expression x=x&(x-1) once will remove the rightmost 1 in the binary system of x. Before x becomes 0, how many times the expression can be executed will remove a few 1s. , so this code implements the function of finding the number of 1's in the two's complement of a signed integer. We know that the complement of -1 is all 1's, and the int type has 4 bytes and 32 bits. Choose C

Question answer:
C


5. Symbolic calculations

The output result after running the following program is ( )

int main()
{
    
    
    int a=1,b=2,m=0,n=0,k;
    k=(n=b<a)&&(m=a);
    printf("%d,%d\n",k,m);
    return 0;
}

A: 0,0 B: 0,1 C: 1,0 D: 1,1

Question analysis:
k=(n=b<a)&&(m=a); The execution order of this part is as follows: execute the n=b<a part first. Among them, the relational operator has a higher priority than the assignment operator, so first If b<a is calculated, 0 is obtained. The result of the n=0 assignment operation will be used as the result of the expression in the brackets, that is, (n=b<a)&&(m=a) is converted into (0)&&(m=a), The expression before the && operation is false, then the following brackets (m=a) are not operated, and the value of m is still 0. Finally, the result of && is 0, that is, k=0

Question answer:
A


6. No need for addition, subtraction, multiplication and division.

Question link: OJ link

Question analysis:
Idea of ​​decimal addition: 15+07, first calculate the addition result of 12 without considering the carry (because the result of 5+7 without considering the carry is 2, if there is a carry of 10), then calculate the carry of 5+7, the carry is 10, then 10 and 12 are added again to get 22, the carry is 0, and the calculation ends here. Binary summation is used here, the idea is similar, but binary calculation addition and carry do not need to use the + symbol. Binary addition idea: the same as decimal, first calculate the addition result without considering carry (0+0 gets 0, 1+1 Carry results in 0, 1+0 results in 1), which can be obtained by using XOR; and then calculate the carry result of the addition (the same position as 1 can be shifted one position to the left), which can be obtained by using phase AND and then shifting left.
Example: 5: 0101 + 7: 0111
The addition result without considering the carry is 0101^0111 -> 0010
The addition result is 0101&0111 -> 0101 because the carry is shifted left to get 1010
1010 + 0010 The
addition result without taking the carry into account is 1010 ^ 0010 - >
The carry of the addition of 1000 is 1010 & 0010 -> 0010 because the carry is shifted left to get 0100. The addition result of
1000 + 0100
without considering the carry is 1000 ^ 0100 ->
The carry of the addition of 1100 is 1000 & 0100 -> 0000. The carry is 0 to end the operation.

Question answer:

int Add(int num1, int num2 ) {
    
    
    while (num2 != 0) {
    
     
        int tmp = num1 ^ num2;
        num2 = (num1 & num2) << 1;
        num1 = tmp;
    }
    return num1;
}

7. Unsigned judgment

The correct statement about the code is ( )

#include <stdio.h>
int main()
{
    
    
    int x = -1;
    unsigned int y = 2;
    if (x > y)
    {
    
    
        printf("x is greater");
    } 
    else
    {
    
    
        printf("y is greater");
    } 
    return 0;
}

A: x is greater B: y is greater C: Implementation dependent D: Random

Question analysis:
x is a signed number -1, and the memory is all 1. When the signed x is compared with an unsigned number, x will be implicitly type converted and treated as an unsigned number, which is a very large number. Then choose A

Question answer:
A


8. Shift calculation

The output result of the following function is ( )

void func()
{
    
    
int k = 1^(1 << 31 >> 31);
printf("%d\n", k);
}

A: 0 B: -1 C: -2 D: 1

Question analysis:
(1 << 31); Shift 31 bits to the left and fill with 0 on the right side to get 0x80000000, that is, the sign bit is 1 and the others are 0, that is, -2147483648 int k = 1^(
1 << 31 >> 31);
Note that when shifting right here, the sign bit remains 1, and is filled with 1 after shifting right. The result is 0xFFFFFFFF, which is -1, 0x00000001^0xFFFFFFFF, which is 0xFFFFFFFE(-2)

Question answer:
C

9. sizeof macro

The output of the following code is ( )

#
include <stdio.h>
int main()
{
    
    
    int i = 1;
    sizeof(i++);
    printf("%d\n", i);
    return 0;
}

A: 1 B: 4 C: 2 D: 8

Question analysis:
Generally, the operation of expressions is executed at runtime, while sizeof is an operator that is executed during the compilation phase. Any operation within it is not executed. Only the type of the expression result is inferred to find its size. Therefore, the value of i before and after remains unchanged.

Question answer:
A

10. Shift calculation

The output result of the following function is ( )

void func()
{
    
    
int k = 1^(1 << 31 >> 31);
printf("%d\n", k);
}

A: 0 B: -1 C: -2 D: 1

Question analysis:
(1 << 31); Shift 31 bits to the left and fill with 0 on the right side to get 0x80000000, that is, the sign bit is 1 and the others are 0, that is, -2147483648 int k = 1^(
1 << 31 >> 31);
Note that when shifting right here, the sign bit remains 1, and is filled with 1 after shifting right. The result is 0xFFFFFFFF, which is -1, 0x00000001^0xFFFFFFFF, which is 0xFFFFFFFE(-2)

Question answer:
C

11. Shift calculation

Please read the following program, the result of which is ( )

int main()
{
    
    
    char c='A';
    if('0'<=c<='9') printf("YES");
    else printf("NO");
    return 0;
}

A: YES B: NO C: YESNO D: Statement error

Question analysis:
'0'<=c<='9' does not determine that x is greater than or equal to character 0 and less than or equal to character 9. Instead, it executes '0'<=c first, uses the result of this expression and then compares it with '9' , the ASCII code value of '0' is 48, and the ASCII code value of 'A' is '65', so '0'<c is the true value 1, 1 is undoubtedly less than the character '9', and it is ultimately true

Question answer:
A

12. Priority Judgment

In C language, the following operators have the highest precedence ( )
A: !
B: %
C: >>
D: ==

Question analysis:
The priority of unary operators is usually relatively high. For specific circumstances, please refer to the operator priority table
priority table:
Insert image description hereInsert image description hereInsert image description here

Question answer:
C

13. Word inversion

Question link: OJ link
Insert image description hereInsert image description here

Question analysis:
For this question, you can first reverse the single sentence between every two spaces, and finally output it in reverse order, so that the words can be arranged in reverse order.

Question answer:

#include <stdio.h>
#include <string.h>
int main() {
    
    
    char arr[10001];//接收字符串
    while (gets(arr) > 0) {
    
    
        char*dete=arr;
        while(*dete!='\0'){
    
    //利用dete指针将字符串中的非字母符号变为空格
            if((*dete >= 'a' && *dete <= 'z') || (*dete >= 'A' && *dete <= 'Z'))
                dete++;
            else{
    
    
                *dete=' ';
                dete++;
            }           
        }
        char*left=arr;//单句左指针
        char*right=arr;//单句右指针
        char*jb=arr;//循环结束判断指针
        while(*jb!='\0'){
    
    
            while(*right != ' ' && *right != '\0')//right指针找到单句右边的第一个空格或\0
               right++;
            jb=right;//将right的地址给到jb,如果为\0则说明字符串单句逆序完成
            while(*left == ' ')//left指针找到单句左边第一个字母
               left++;
            char*moveleft=left;//moveleft指向单句左边第一个字母
            char*moveright=right-1;//moveright指向单句右边第一个字母
            while(moveleft<moveright){
    
    //单句逆序
                char temp=*moveleft;
                *moveleft=*moveright;
                *moveright=temp;
                moveleft++;
                moveright--;
            }
            left=right;//让left继承right指针的位置
            while(*right == ' ')//将right指针指向下一个单句的首字母位置
               right++;
        }
        char*ptr=left-1;//结束单句逆序时left指针指向\0,则让ptr指向left-1,即最后一个单词的位置
        while(ptr>=arr){
    
    //逆序输出
            printf("%c",*ptr);
            ptr--;
        }
    }
    return 0;
}           

Summarize

Say important things three times!
progress! progress! progress!

Guess you like

Origin blog.csdn.net/mdjsmg/article/details/132577384