1. Given a sequence of A1, A2, A3,…, consisting of n integers (which may be negative integers ,......, An. Calculate the maximum value of the field and of the sequence.
Method: Dynamic Planning
Int maxdp (int * a, int Len)
{
Int I, max = 0, B = 0;
If (A [0]> 0)
{
B = A [0]; // you can release the function based on Len and new, or use it as a global variable.
}
For (I = 1; I <Len; I ++)
{
B = (a [I] + B> 0 )? (A [I] + B): 0;
If (B> MAX)
{
Max = B;
}
}
Return Max;
}
O (n) time complexity, O (n) space complexity.
2. The non-adjacent subsequence with the greatest sum in a positive integer sequence (any two elements of the sequence are not adjacent in the original sequence column ).
Method: Recursion
Int CAL (INT H1, int length, unsigned int * B) // when the length is greater than 2, H1 needs to start from-2.
{
If (length-h1 = 1) // recursion to the last number, because it is a positive number, select
Return B [H1];
If (length-h1 = 2) // recursion to the last two numbers, select the big one
Return (B [H1]> B [H1 + 1]? B [H1]: B [H1 + 1]);
If (length-h1 = 3) // recursion to the last three numbers, select one in the middle or two on both sides.
Return (B [H1] + B [H1 + 2]> B [H1 + 1]? B [H1] + B [H1 + 2]: B [H1 + 1]);
If (h1> = 0 )//
Return B [H1] + (CAL (H1 + 2, length, B)> CAL (H1 + 3, length, B )? Cal (H1 + 2, length, B): Cal (H1 + 3, length, B ));
Else // H1 starts from-2, so the values of [0, length-1] and [1, length-1] are compared.
Return (CAL (H1 + 2, length, B)> CAL (H1 + 3, length, B )? Cal (H1 + 2, length, B): Cal (H1 + 3, length, B ));
}
3. EMC recursion:
Int func (int I, int N );
Where I <= N, the function outputs an integer that increments from I to N and then to I. Each row outputs a number. For example, func (1, 5) is
1
2
3
4
5
4
3
2
1
Requirements
1 can have only one statement, that is, a semicolon
2. The do while until goto for if keyword cannot be used? : And comma Operator
3. The only library function that can be used is printf.
Int func (int I, int N)
{
Return (I = N & printf ("% d \ n", I) | (printf ("% d \ n", I) & func (I + 1, n) & printf ("% d \ n", I )));
}
4.Enter the head node of a linked list and output the value of each node from the end to the end..
The linked list node is defined as follows:
Struct listnode
{
Int m_nkey;
Listnode * m_pnext;
};
Analysis: this is an interesting interview question. This question and its variants often appear in interviews and pen questions of major companies. It is implemented using recursion. To output the linked list in turn, each time we access a node, we first recursively output the node next to it, and then output the node itself, so that the output result of the linked list is reversed.
Void printlistreversely (listnode * plisthead)
{If (plisthead! = NULL)
{//
Print the next node first
If (plisthead-> m_pnext! = NULL)
{
Printlistreversely (plisthead-> m_pnext );
}
// Print this node
Printf ("% d", plisthead-> m_nkey );
}
}
5. Implement the link of two ordered linked lists using recursive methods.
Node * mergerecursive (node * head1, node * head2)
{
If (head1 = NULL)
Return head2; // If head1 is null, head2.
If (head2 = NULL)
Return head1; // If head2 is null, head1.
Node * head = NULL; // It is used to store small nodes in head1 and head2.
If (head1-> data {
Head = head1;
Head-> next = mergerecursive (head1-> next, head2 );
}
Else
{
Head = head2;
Head-> next = mergerecursive (head1, head2-> next );
}
Return head;
}
6. Use recursive methods to reverse a single-chain table:
Link reverselist (link * root, link P)
{
Link T = NULL;
If (p-> next! = NULL) // if it does not reach the end of the linked list, call it recursively
T = reverselist (root, p-> next );
Else // if it has reached the end of the linked list, change next of the first vertex of the linked list to null, and change the first node to the last node,
{
(* Root)-> next = NULL;
* Root = P;
}
If (T! = NULL)
{
T-> next = P;
}
Return P;
}
7. Enter a string to print all the characters in the string.
For example, if the input string ABC is used, all the strings ABC, ACB, Bac, BCA, cab, and CBA that can be arranged by characters A, B, and C are output.
Analysis: This is a good question about recursive programming. It is also difficult to understand. recursion and loop adding make me dizzy...
Void permutation (char * pstr, char * pbegin );
Void permutation (char * pstr)
{
Permutation (pstr, pstr );
}
Void permutation (char * pstr, char * pbegin)
{
If (! Pstr |! Pbegin)
Return;
If (* pbegin = '\ 0 ')
{
Printf ("% s \ n", pstr );
}
Else
{
For (char * PCH = pbegin; * PCH! = '\ 0'; ++ PCH)
{
// Swap PCH and pbegin
Char temp = * PCH;
* PCH = * pbegin;
* Pbegin = temp;
Permutation (pstr, pbegin + 1 );
// Restore PCH and pbegin
Temp = * PCH;
* PCH = * pbegin;
* Pbegin = temp;
}
}
}
8.
Select all the combinations of M (0 <m <= N) numbers in N numbers. The problem can be divided:
1. First, select the maximum number of N numbers, and then select the number of m-1 in n-1-1 numbers until 1 is selected from the number of N-(m-1.
2. Select a smaller number from N to continue with Step 1 until the maximum number of available numbers is M.
Obviously, the above method is a recursive process. That is to say, the recursive method can be used to obtain all combinations cleanly.
The implementation of recursive methods is as follows:
/// Obtain all the combinations of any M elements from array a [1 .. n.
/// A [1. N] indicates the candidate set. N indicates the large and small candidate set, and N> = m> 0.
/// B [1. m] is used to store the elements in the current combination (the element subscript is stored here ),
/// Constant M indicates the number of elements in a combination that meets the conditions. M = M. These two parameters are only used to output results.
Void combine (int A [], int N, int M, int B [], const int m)
{
For (INT I = N; I> = m; I --) // pay attention to the loop range.
{
B [M-1] = I-1;
If (M> 1)
Combine (A, I-1 m-1, B, m); // M-1 from the number of I-1.
Else // M = 1, output a combination
{
For (Int J = M-1; j> = 0; j --)
Cout <A [B [J] <"";
Cout <Endl;
}
}
} This question is difficult to write comments, but it is better to trace and debug it by yourself.
9. Eight queens question: put eight queens on 8x8 chess so that they cannot attack each other, that is, the two queens are not allowed to be on the same row, column, or diagonal slashes. Each black lattice in represents a queen, which is a qualified placement method. The total number of requests.
Because any two of the eight queens cannot be in the same row, this must be because every queen occupies one row. Therefore, we can define an array columnindex [8]. The number I in the array indicates the Queen's column number in the row I. First, initialize the eight columnindex numbers with 0-7, respectively. Next we need to arrange the columnindex in full. Since we use different numbers to initialize numbers in the array, any two queens must have different columns. We only need to determine whether the eight queens corresponding to each arrangement are on the same diagonal slashes, that is, the two subscripts I and J of the array, is it I-j = columnindex [I]-column [J] Or J-I = columnindex [I]-columnindex [J]?
Int g_number = 0;
Void eightqueen ()
{
Const int queens = 8;
Int columnindex [queens];
For (INT I = 0; I <Queens; ++ I)
Columnindex [I] = I;
Permutation (columnindex, Queens, 0 );
}
Void permutation (INT columnindex [], int length, int index)
{
If (Index = length)
{
If (check (columnindex, length ))
{
++ G_number;
Printqueen (columnindex, length );
}
}
Else
{
For (INT I = index; I <length; ++ I) // The following is exactly the same as the preceding arrangement.
{
Int temp = columnindex [I];
Columnindex [I] = columnindex [Index];
Columnindex [Index] = temp;
Permutation (columnindex, length, index + 1 );
Temp = columnindex [Index];
Columnindex [Index] = columnindex [I];
Columnindex [I] = temp;
}
}
}
Bool check (INT columnindex [], int length)
{
For (INT I = 0; I <length; ++ I)
{
For (Int J = I + 1; j <length; ++ J)
{
If (I-j = columnindex [I]-columnindex [J]) | (J-I = columnindex [I]-columnindex [J]) // check whether any two elements are not on the same diagonal line.
Return false;
}
}
Return true;
}
Void printqueen (INT columnindex [], int length)
{
Printf ("solution % d \ n", g_number );
For (INT I = 0; I <length; ++ I)
Printf ("% d \ t", columnindex [I]);
Printf ("\ n ");
}
10. Find the depth of a binary tree:
Struct Node
{
Node * left;
Node * right;
};
Int getdepth (node * root)
{
If (null = root)
Return 0;
Int left_depth = getdepth (root-> left );
Int right_depth = getdepth (root-> right );
Return left_depth> right_depth? Left_depth + 1: right_depth + 1;
}