Daily questions-6

Table of contents

1. Multiple choice questions

2. Algorithm questions

1.Fibonacci sequence

2. Judgment of legal bracket sequence


1. Multiple choice questions

1、

Analysis: Inline functions are a method that can improve function execution efficiency. Its principle is to directly expand the code of the function body at the function call point during compilation , thus avoiding the overhead of function calls.

However, inline functions also have some limitations and disadvantages, such as:

  • Inline functions cannot contain complex structural control statements , such as loop statements and switch statements, otherwise it will lead to code bloat and reduced efficiency.
  • Inline functions cannot be directly recursive functions , that is, they also call their own functions internally, otherwise they will cause infinite loops or stack overflows.
  • The definition of an inline function must appear before the inline function is called for the first time , otherwise the compiler cannot expand the function body.
  • Inlining a function is just a suggestion to the compiler , and the compiler can decide whether to actually inline it based on the function's complexity and frequency of calls.

Therefore, it is suitable to use inline functions when the function code is small, frequently called, and without complex flow control and recursive calls. This can maximize the advantages of inline functions and improve the running speed of the program. The answer is C.

2、

Analysis: Default parameters (default parameters): When declaring and defining a function, you can bring a default value to the parameters of the function; when calling the function, if the user does not pass actual parameters, the default value given when defining is used. If the user passes arguments, the arguments passed by the user are used.
Full default parameters: each parameter has a default value;
semi-default parameters: some parameters have default values, and the default values ​​must be given from right to left, for example:

void f(int a, int b = 20, int c = 10) compiles successfully. 
void f(int a = 10, int b, int c = 20) fails to compile. The answer is D.

3、

Analysis: There are two ways to define a class:
1. All declarations and member function definitions can be placed in the class.
2. Only the declarations of member variables and member functions can be placed in the class. The definition of member functions can be defined in a .cpp file. Note: The class name must be added before the member function name.
class: default access rights private; struct: default access rights public.
Suggestion: Set member variables to private and member functions to public. The answer is A.

4、

Analysis: The characteristics of the constructor are as follows:

  • The name of the constructor must be the same as the class name and is case-sensitive;
  • The constructor has no return value and cannot be modified with void ;
  • Constructors can be modified with any access modifier (public, protected, and private);
  • The constructor cannot be modified with keywords such as static, final, abstract, and synchronized;
  • Constructors cannot be overridden;
  • Constructors can be overloaded, distinguished by the number, type and order of parameters; overloading means that there can be multiple constructors in a class. Constructor 1 can call constructor 2 to complete object initialization, through the this key Word: this (parameter 1, parameter 2...) implementation; the answer is C.

5、

Analysis: Generally speaking, using an initialization list is more efficient than assigning values ​​inside the constructor, because the initialization list can directly call the constructor of the member variable without first calling the default constructor and then assigning the value. Moreover, in some cases, initialization
(1) constant members must be used, because constants can only be initialized but not assigned , so they must be placed in the initialization list
(2) Reference types, references must be initialized when defined and cannot be reassigned , so they are also It should be written in the initialization list
(3) Class types without default constructors , because using the initialization list does not require calling the default constructor for initialization, but directly calls the copy constructor or other appropriate constructor for initialization. The answer is B.

6、

Analysis: If the operator is overloaded into a member function of the class, the number of formal parameters is 1 less than the number of parameters required by the operator, because the member function has a hidden this pointer. If the class has a single-parameter constructor, the constructor has the function of type conversion. For option B, the compiler will call the single-parameter constructor to convert 3 to a BigNumber object when
compiling the code node , but the first parameter of option D is It is not an object, there is no this pointer, so it is wrong,

 

7、

Analysis: A friend function is a special function in C++ that has permission to access private members of a class, even if the function is not a member function of the class. Friend functions can be declared as friends inside a class or as friends outside the class.
D is wrong. The friend function is modified with the keyword friend, but it is not called through the this pointer when it is called, because the friend function does not belong to any class and does not have this pointer.
 

2. Algorithm questions

1.Fibonacci sequence

Analysis: Find two Fibonacci numbers such that F[i]<=n<=fF[i+1], and then calculate the distance between n and the two numbers.

Code:

#include <iostream>
using namespace std;

int main() {
    int n;
    while(cin>>n)
    {
        int f0=0,f1=1,f2=1;
        int step=0;
        while(n>f2)
        {
            f0=f1;
            f1=f2;
            f2=f1+f0;
        }
       step=(f2-n)<(n-f1)?(f2-n):(n-f1);
       cout<<step;

    }
}

2. Judgment of legal bracket sequence

Analysis: This question examines the application of the stack. Implemented using a stack structure, the left bracket is stored in the stack. When a right bracket is encountered, check whether there is a left bracket in the stack. If there is a left bracket, pop it off the stack. If not, it means there is no match. After traversing the string completely, check whether the stack is empty. If it is empty, it means it is a legal bracket sequence.

Code:

class Parenthesis {
  public:
    bool chkParenthesis(string A, int n) {
        stack<char> sc;
        for (auto ele : A) {
            switch (ele) {
                case '(':
                    sc.push(ele);
                    break;
                case ')': {
                        if (sc.empty() || sc.top() != '(')
                            return false;
                        else
                            sc.pop();
                    }
                    break;
                default:
                    return false;
            }
        }
        return true;
    }
};

Guess you like

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