Daily question-1

Table of contents

1. Multiple choice questions

2. Programming questions

1. Team competition

2. Delete public characters



1. Multiple choice questions

1、

Analysis: %s will stop when it encounters '\0'. m in %m.ns represents the string width, n represents n characters of the target string from the left, right-aligned, (if you want left-aligned, you can write %- m.ns) is not enough to fill in spaces. Note: If n>m, then m will be invalid, and the output width is n. If n>string length, the entire string will be output, no matter what m is.

So the answer to this question is computer, com.

2、

Analysis: When different types of data are involved in operations in an expression , the compiler will automatically convert the smaller type into a larger type and then perform calculations. In this example, char and int are both smaller types, and double is a larger type, so 'A' and a will be converted to double types, and then added to b to get a double type result. Choose C for this question.

3、

Analysis: When free is used to release a pointer, free will not automatically set the pointer to NULL. It only releases the memory space of the pointer. The programmer also needs to explicitly assign NULL to the pointer variable. Choose A for this question.

4、

Analysis: The use of const in pointers follows this rule,

(1) If const is on the left side of the asterisk, then const modifies the variable pointed by the pointer , that is, the pointer pointed to is a constant. For example, const int*p and int const*p both indicate that p is a pointer to const int. p can change the pointer, but the value of the variable cannot be changed through p.

(2) If const is on the right side of the asterisk, then const modifies the pointer itself, that is, the pointer itself is a constant. For example, int *const p means that p is a constant pointer pointing to int. p cannot change the pointer, but the value pointed to can be changed through votes.

(3) If const is on the left and right of the asterisk at the same time, then neither the point of p nor the value pointed to can be modified through p, such as const int* const p.

Therefore, there is the following error in the program:

 (1) Line: The constant pointer p1 is not initialized, an initial address should be given, and it must be a variable address. (The answer is missing this)

 (6) Line: The constant pointer p2 cannot modify the value of the variable it points to. This line should be removed.

(7) Line: The pointer constant p3 cannot change the pointer, this line should be removed.

5、

Analysis: Macro-defined constants are not necessarily easier to understand than const constants, because macro-defined constants have no type information and may be repeatedly defined or undefined . In addition, const constants can avoid operation precedence or side effects that may be caused by some macro definitions. Choose B for this question.

6、

Analysis: An array with 10 pointers can int *a[10]be used to declare, which means it ais an array whose elements are pointers to integers. A pointer to a function can int (*a)(int)be declared, which means aa pointer to a function that takes an integer argument and returns an integer.

So, if we want an array of 10 pointers, all pointing to the same type of function, we can int (*a[10])(int)use declaration, which means aan array whose elements are pointers to an integer parameter and return an Pointer to an integer function.

7、

Analysis: On a 32-bit CPU, with default alignment, the total size of the structure is an integer multiple of the size of the widest basic type member of the structure . In this example, the widest basic type member of the structure is unsigned int, which takes up 4 bytes. Bitfield members in a structure are aligned according to the base type to which they belong.

In this example, all bitfield members are of type unsigned int, so they are aligned to 4 bytes. If the bit field members in the structure are not divisible by the basic type to which they belong, they will be stored starting from the next basic type. In this example, a, b, and c occupy a total of 19+11+4=34 bits, so c is placed in the second 4 bytes, and 4+29=33 bits, so d is placed in the third 4 bytes. in.

Therefore, the structure occupies a total of 3 unsigned int and 1 char, occupying a total of 3*4+1=13 bytes. However, since the total size of the structure must be an integer multiple of 4, 3 bytes of padding need to be added at the end, making the total size of the structure 16 bytes.

8、

Analysis: The difference between &a and a mainly depends on the type of a and the context of use. Generally speaking, &a means taking the address of a, and a means taking the value of a. However, if a is an array or a function, the difference is more complicated.

If a is an array, such as int a[10], then &a and a both represent the first address of the array , but their types are different. The type of &a is a pointer to the array , that is, int(*)[10], and the type of a is a pointer to the first element of the array , that is, int *. Therefore, the values ​​​​of &a+1 and a+1 are also different. &a+1 means skipping the entire array, while a+1 means skipping one element .

If a is a function, such as int a(int x), then &a and a both represent the address of the function , but their types are different. The type of &a is a pointer to a function , that is, int (*)(int), while the type of a is a function type , that is, int (int). Therefore, when calling a function, you can directly use a(x) or (*a)(x), or you can use (&a)(x) or (**&a)(x), etc.

Now let’s look at the topic:

&ais a pointer to an array , its type is int (*)[4], that is, a pointer to an array with 4 integer elements.

&a+1It is to &amove backward one unit, that is, the length of the array is moved , which is the length of 4 integers, that is, 4*4=16 bytes.

(int*)(&a+1)is to be &a+1cast to a pointer to an integer, its type is int *, that is, a pointer to an integer.

ptr=(int*)(&a+1)It is (int*)(&a+1)assigned to ptr, so ptrit points to athe first position after the array, which is athe position in memory where the next variable of the array is stored.

*(ptr-1)It is to ptrmove forward one unit, that is, move the length of an integer, that is, 4 bytes, and then take out the value it points to. This is equivalent to taking out athe last element of the array, which is 4.

2. Programming questions

1. Team competition

Analysis: The main idea of ​​​​this question is the greedy algorithm. The greedy algorithm is actually very simple. It is to choose the local optimal solution that can be seen every time when selecting a value. Therefore, the greed here is to ensure that the second value of each group can be obtained. Just choose the maximum value. We try to take the maximum value every time, but the maximum number cannot be the median, so the next best thing is to take the second largest number in each group.

code show as below:

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
    int n;
    while(cin>>n)
    {
        long long sum=0;
        vector<int> team;
        team.resize(3*n);
        for(int i=0;i<3*n;i++)
        {
            cin>>team[i];
        }
        sort(team.begin(),team.end());
        for(int i=0;i<n;i++)
        {
            sum+=team[team.size()-(2*i+1)-1];
        }
        cout<<sum;
    }  
}

2. Delete public characters

Analysis: If you use the traditional brute force search method for this question, such as judging whether the character of the first string is in the second string, and then moving the character to delete the character, the efficiency will be O(N^2), which is too low. Hardly satisfying.
1. Map the characters of the second string to a hash array to determine whether a character is in this string.
2. To determine if a character is in the second string, do not use deletion. This is too inefficient because each deletion is accompanied by data movement. Here you can consider adding characters to a new string and finally returning the new string.

code show as below:

#include <iostream>
#include<string>
using namespace std;

int main() {
    string str1,str2;
    getline(cin,str1);
    getline(cin,str2);

    int hash[256]={0};

    for(int i=0;i<str2.size();i++)
    {
        hash[str2[i]]++;
    }
    string newstr="";
    for(int i=0;i<str1.size();i++)
    {
        if(hash[str1[i]]==0)newstr+=str1[i];
    }
    cout<<newstr<<endl;

    
}

Guess you like

Origin blog.csdn.net/weixin_65592314/article/details/132683847