Programmer problem set

Source: Internet
Author: User

Q: Define a macro to compare the sizes of two numbers A and B. The statements greater than, less than, and if cannot be used.
A: Define max (a, B) (a/B )? A: B

// Configure //---------------------------------------------------------------------------------------

Q: Write a virus.
A:
While (1)
{
Int * P = new int [10000000];
}

// Configure //---------------------------------------------------------------------------------------

Q: I have four people, A, B, C, and D. I want to have a bridge at night. It takes 1, 2, 5, and 10 minutes for them to pass through the bridge.
One flashlight, and at most two people can bridge the bridge together. How can I arrange for the four to cross the bridge within 17 minutes?
A:
Answer:
Step 1: A (1) and B (2) Bridge, A (1) returns cost: 1 + 2
Step 2: C (5) and D (10) Bridge, B (2) return cost: 10 + 2
Step 3: bridge A (1) and B (2) Cost: 2

// Configure //---------------------------------------------------------------------------------------

Q: implement the strlen function. No variables are allowed ~
A:
# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string>

Using namespace STD;

Int mystrlen (const char * Str)
{
Return * Str? (* (STR + 1 )? (1 + mystrlen (STR + 1): 1): 0;
}

Int main ()
{
Char * orig = "Hello, world! ";

Cout <orig <"(char *)" <Endl;
Cout <mystrlen (orig) <Endl;

Return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
Set * Str? (* (STR + 1 )? (1 + mystrlen (STR + 1): 1): 0 expanded

Int mystrlen (const char * Str)
{
If (* Str! = 0)
{
If (* (STR + 1 )! = 0)
{
Return (1 + mystrlen (STR + 1 ));
}
Else
{
Return 1;
}
}
Else
{
Return 0;
}
}

// Configure //---------------------------------------------------------------------------------------

Q: use recursion to write X! = X * (x-1) * (X-2) * (X-3) *... * 2*1
A:

Unsigned short factorial (unsigned short );

Int main (void)
{
Int num;
Int total;
Cout <"Enter data:" <Endl;
Cin> num;
Total = factorial (Num );
Cout <"Result:" <total <Endl;
Return 0;
}

Factorial ()
{
If (A = 1)
{
Return 1;
}
Else
{
A * = factorial (A-1 );
Return;
}
}

// Configure //---------------------------------------------------------------------------------------

Q: Which of the following are equivalent?
Int B;
A const int * A = & B;
B const * int A = & B;
C const int * const A = & B;
D int const * const A = & B;

A: (C = d) both of them are equivalent to constant pointer constants (the pointer itself is a constant and the content is also a constant)
A const int * A = & B; // constant pointer. It can only read memory data, but cannot modify memory data.
B const * int A = & B; // invalid.
C const int * const A = & B; // the address of the memory data and pointer cannot be modified.
D int const * const A = & B; // the address of the memory data and pointer cannot be modified.
E int * const A = & B; // pointer constant. The pointer address cannot be modified and can only point to B.

// Configure //---------------------------------------------------------------------------------------

Q: Do inline functions perform parameter type check during compilation?
Void g (base & B)
{
B. play;
}

Void main ()
{
Son S;
G (s );
Return;
}

A:
Inline functions are used to check the parameter type, which is an advantage of inline functions over macros.

// Configure //---------------------------------------------------------------------------------------

Q: Which of the following methods can be used to transmit the values of C ++ functions?
A:
There are three transfer methods for the value of the C ++ function: 1. value transfer 2. pointer transfer 3. Reference Transfer

// Configure //---------------------------------------------------------------------------------------

Q: What is the role of the header file?
A:
1. To call the library function through the header file, you only need to call the library function according to the interface declaration in the header file, without having to worry about how the interface is implemented, the compiler will extract the corresponding code from the library.
2. Enhance the type security check. When a function or interface is used, the method is inconsistent with the Declaration in the header file, and the compiler will cause errors.

// Configure //---------------------------------------------------------------------------------------

Q: How many memory allocation methods are available?
A:
1. From the static storage area, the memory has been allocated during compilation. The program exists throughout the runtime, such as global variables and static variables.
2. Create on the stack. During function execution, the storage units of local variables in the function can be created on the stack. When function execution ends, these storage units are automatically released. Stack allocation operations are embedded in the instruction set of the processor, which is highly efficient, but the memory allocation capacity is limited.
3. Allocate from the stack, also known as dynamic memory allocation. When the program runs, the program uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete at any time.

// Configure //---------------------------------------------------------------------------------------

Q: two-way linked list is implemented to delete a node P, insert a node after node P, and write these two functions;
A:
Typedef struct dnode
{
Int date;
Dnode * Prior;
Dnode * next;
};

Dnode * Dlist;

Void insertnode (Dlist & list, int pvalue)
{
Dlist P = List;

If (P = NULL)
Return;

While (p-> date! = Pvalue)
{
P = p-> next;
}

Dnode * q = new dnode ();

P-> date = pvalue;
P-> next-> prior = Q;
Q-> next = p-> next;
P-> next = Q;
Q-> prior = P;

}

Void deletenode (Dlist & list, int pvalue)
{
Dlist P = List;

If (P = NULL)
Return;

While (p-> date! = NULL)
{
P = p-> next;
}

P-> prior-> next = p-> next;
P-> next-> prior = p-> prior;

// Free P;
Delete P;
}

// Configure //---------------------------------------------------------------------------------------

Q: Is all the operations in C ++ caused by main? If not, give an example.
A:
Of course, not all actions are caused by main (), but the compiler starts to execute main ().
Static variables and global variables are allocated before Main.
Inline, template, function declaration, and macro extension are all precompiled behaviors and not completed by main ().
Note: C ++ and main () have no logical connection.

// Configure //---------------------------------------------------------------------------------------

Q: How do I define and implement a class member function as a callback function?
A:
Declare the member function as static.

// Configure //---------------------------------------------------------------------------------------

Q: In C ++, how do I declare the const void F (void) function as a library function in C programs?
A:
Extern "C" Void F (void );
Therefore, extern "C" applies the C naming convention when the C ++ compiler modifies the function name.

// Configure //---------------------------------------------------------------------------------------

Q: What is the entry point for a Windows program? Write the WINDOWS Message Mechanism process.
A:
The entry point is the winmain function.

Windows message mechanism process:

1. in Windows, there is a system message queue. For every running Windows application, the system creates a "message queue" for it, that is, the application queue, stores messages of various Windows that may be created by the program. The application contains a piece of code called "message loop", which is used to retrieve these messages from the message queue and distribute them to corresponding window functions.

2. Windows maintains a message queue for each currently running Windows program 」. After an input event occurs, Windows converts the event to a message and puts the message in the message queue of the program. The program extracts messages from the message queue by executing a program code called "message loop:

While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}

Translatemessage (& MSG); transmits the MSG structure to Windows for some keyboard conversion.
Dispatchmessage (& MSG); returns the MSG structure to Windows. Then, Windows sends the message to the appropriate window message handler for processing.

What is the difference between sendmessage () and postmessage?
Both are used to send messages to applications. Postmessagex () directly adds a message to the Message Queue of the application, and exits without waiting for the program to return. sendmessage () is the opposite. After the application processes the message, it returns.

// Configure //---------------------------------------------------------------------------------------

Q: Fill in the blanks of a linked list.
A:
Void reverse (test * head)
{
Test * Pe = head;
Test * PS = head-> next;
While (PS)
{
// Method 1 PE is fixed
Pe-> next = ps-> next;
PS-> next = head;
Head = Ps;
PS = pe-> next;

// Method 2 pe ps head move backward
// Head = ps-> next;
// PS-> next = PE;
// Pe = Ps;
// PS = head;
}
}

// Configure //---------------------------------------------------------------------------------------

Q: What functions cannot be virtual functions?
A:
Constructor ()
Inline functions (because there is no function address, it is a static action to insert during compilation .)
Static member functions (because static member functions are similar to global functions but belong to the corresponding class, there is no this pointer under the scope of the corresponding class)

// Configure //--------------------------------------------------------------------------------------

Q: What is the difference between a virtual function and an interface?
A:
Concepts of virtual functions:
Virtual functions are the basis of dynamic Association. They are used to express the relationship between the base class and the member functions of the derived class after the concept is introduced. A virtual function is defined in a base class. It is also a member function and is not a static member function.
If the declaration of an instance method contains a virtual modifier, the method is called a virtual method. The implementation of a virtual method can be replaced by a derived class. The process to replace the inherited virtual method is called overwriting. In a virtual method call, the runtime type of the Instance involved in the call determines which implementation of the method is called.
Limitations of virtual functions:
1. virtual functions are only applicable to Class objects with inheritance relationships. Therefore, only member functions of a class can be described as virtual functions.
2. static member functions cannot be virtual functions.
3. inline functions cannot be virtual functions.
4. the constructor cannot be a virtual function.
5. The Destructor can be a virtual function.

Interfaces can include static members, nested types, abstractions, virtual members, attributes, and events. Any class that implements the interface must provide the definition of the abstract member declared in the interface. The interface can require that any implementation class must implement one or more other interfaces.
The interface has the following restrictions:
The interface can be declared with any accessibility, but all interface members must have public accessibility.
You cannot attach security permissions to members or interfaces.
The interface can define class constructor, but cannot define instance constructor.
Each language must provide rules for ing an implementation for the interface that requires Members, because more than one interface can use the same signature to declare members, and these members can be implemented independently.
Interfaces can be implemented using classes and structures. To implement an interface only by class or structure, the list of base classes of this class or structure should contain the identifier of this interface. If a class or structure implements an interface, it also implicitly implements all the basic interfaces of this interface. This is true even if all base class interfaces are not explicitly listed in the base class list of the class or structure.

// Configure //--------------------------------------------------------------------------------------

Q: How do I implement virtual functions in C ++?
A:
Each class that defines a virtual function has a virtual function table vtbl (virtual function table). This table is actually an array of function pointers and records the entry address of the virtual function. As long as this class has virtual functions, whether defined by itself or inherited from the parent class, each instance of the class-the object has a pointer vptr (virtual function table pointer ), it points to the virtual function table of the class.
The following three conditions of the virtual function (polymorphism) mechanism:
1. Derivative relationship
2. Virtual attributes
3. Pointer --> access method

// Configure //---------------------------------------------------------------------------------------

Q: there are 12 small balls in the same shape. The quality of one ball is different from that of the other 11. Give a balance, how can I find this ball three times and find out whether the ball is lighter or heavier than others?

A:
Number the 12 balls A1, A2, a3...... A10, A11, A12.
Step 1: Separate the 12 balls by 3, with each dial of 4, A1 ~ The first dial in A4, marked as B1, A5 ~ A8 2nd dial, marked as B2, and other 3rd dial, marked as B3;
Step 2: Place B1 and B2 on the two disks of the balance, and set the left disk to C1 and the Right to C2. At this time, the two conditions are as follows:

1. c1 and c2 are balanced. At this time, we can determine that both A1 and A8 are regular balls. Then, we can leave C2 empty, take A4 from C1, and take three balls from A9 to A12, assume that it is A9 to A11 and put it on C2. In this case, C1 is A1 to A3, C2 IS A9 to A11. There are three situations:
A: the balance of the balance is very simple. It means that the A12 that has not been placed is the same ball, but it has been called twice in this step. Therefore, the A12 is called once again with 11 regular balls, that is, the third time, you can immediately determine whether the A12 is heavy or light;
B: If C1 rises, it indicates that the isoball is one of the three balls from A9 to A11 and is heavier than the regular ball. Remove all the balls of C1, place A8 on C1, remove A9, and compare A8 and A11 (called the third time ), if the balance is set, the A9 removed from C2 is biased towards the same ball. If the balance is not balanced, the disk is biased towards the same ball;
C: If C1 drops, one of the A9 to A11 is slightly different. The next step is to copy B;

2. C1 and C2 are not balanced. At this time, C1 increases and C1 decreases. However, either case indicates that A9 to A12 is a regular ball. This step is the key to solving the problem. This is also the best part of the question.
A: When C1 rises, you cannot determine which disk of the same ball is or whether it is light or heavy. Remove A2 from C1 to A4, put A5 and A6 from C2 to C1, and then place A9 from the regular ball to C2. So far, C1 is A1, A5 and A6, C2 IS A7, A8 and A9. At this time, it is divided into three situations:

1) if the balance is exceeded, it means that all balls on the balance are regular balls. The different balls remove A2 to A4 from C1. And we can determine the severity of Different balls. Because A5 to A8 are both regular balls, and C1 is rising in 2nd times, so A2 to A4 must have a light ball. The third term is used to find the light ball from A2 to A4. This is very simple. If you take two balls and place them in C1 and C2, the remaining balance is to seek the ball. If you are not balanced, the lower side is to find the ball;

2) If C1 continues to rise, it means either A1 is the light ball to be sought, or both A7 and A8 are the heavy balls (Do you understand this step? Think about it. It's very easy. Because A9 is a regular ball, And the A2 to A4 removed must also be a regular ball, you can also release the A5 and A6 which can be placed on the disk. So either A1 is light or A7 or A8 is heavy ). At this point, there is only one chance to claim it. You only need to put A7 and A8 on two disks. Balancing means that A1 is looking for a slightly different ball. If it is not balanced, which side of the ball is high indicates which one is focusing on the same ball;

3) if you change the ball, it means that the balance is broken after 2nd times, and C1 is reduced, it means that the same ball is definitely in the A5 and A6 values, and the same ball is biased, otherwise, the balance is either balanced or C1 increases. After determining that the ball to be searched is biased, put A5 and A6 on two disks and say that the A5 and A6 can be determined based on the height of 3rd Times;

B: After 1st calls, C1 decreases. At this time, C1 can be regarded as C2. In fact, the subsequent steps are the same as that of a, so it is unnecessary to repeat the description. At this point, no matter what the situation is, and only three times can be used to name 12 balls with the same looks and feel, with the Quality Being different from that of the other 11 balls. In addition, you can determine whether it is light or heavy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.