Programmer interview question 100 (1)

Source: Internet
Author: User

The question is selected from the following blog website:
Http://zhedahht.blog.163.com /#.

Question 26th:
Question: enter a positive number N, and output all sequences that are n continuous positive numbers.

For example, input 15, because 1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8 = 15, therefore, three consecutive sequences 1-5, 4-6, and 7-8 are output.

Analysis: This is Netease's interview question.

My own ideas:
The first thought is the sum of the arithmetic difference series, that is (M1 + (M1 + k-1) * k = N * 2, where the start number start = m1, end number end = m1 + k-1;
Therefore, the solution of this question is related to the factor n * 2. The set of solutions is related to several factors in the range [2, SQRT (2n)] of 2n. Each factor may correspond to a solution, but it may not.

Int IMAX = ( Int ) Math. SQRT (n * 2 ) + 1 ;
For ( Int I = 2 ; I < IMAX; I ++ )
{
If (N * 2 ) % I = 0 )
{
// We found a factor where I is 2n.
Temp = N * 2   - I * I + I;
If (Temp % (I * 2 ) = 0 )
{
// We found that this factor is indeed a solution.
Start = Temp / (I * 2 );
End = Start + I - 1 ;
Console. writeline ( " {0}-{1} " , Start, end );
}
}
}

When N = 30000, the output result is as follows:

9999-10001
5998-6002
1993-2007
1188-1212
922-953
363-437
265-360
178-302
108-267

I think its advantages are:CodeA small amount, clear and easy to read.

----------------------------------------
Question 23rd: Step jumping
Question: a step has a total of N levels. If you can skip one level at a time, you can also skip two levels. Calculate and analyze the total number of hops in totalAlgorithmTime complexity.

Analysis: This question has frequently appeared recently. Companies that focus on algorithms, such as microstrategy, have chosen this question as an interview question or a pen question.

First, consider the simplest situation. If there is only one level, there is obviously only one method to skip. If there are two steps, there are two ways to skip: one is to skip level 1 each time, and the other is to skip level 2 at a time.

Now let's discuss the general situation. We regard the jump Method for n-level steps as a function of N and record it as F (n ). When N> 2, there are two different options for the first hop: one is the first hop with only one level. At this time, the number of hops equals to the number of hops for the next n-1 level step, that is F (n-1); another option is the first jump level 2, at this time the number of Jump method is equal to the number of the next step of the N-2 level, that is, F (n-2 ). Therefore, F (n) = f (n-1) + (F-2) of different hops at N-level steps ).

We will summarize the above analysis using a formula as follows:

/1 n = 1
F (n) = 2 n = 2
\ F (n-1) + (F-2) n> 2

From this analysis, I believe many people can see that this is the familiar sequence of fiber ACCI.

The solution to the problem is a tree structure. I introduced the node treenode IN THE Treeview to generate this tree. It is a typical recursion:

// Assemble a root node into a tree
Static   Void Addnode (system. Windows. Forms. treenode root, Int N)
{
If (N > 2 )
{
Root. nodes. Add ( " 1 " );
Root. nodes. Add ( " 2 " );
Addnode (root. nodes [ 0 ], N - 1 );
Addnode (root. nodes [ 1 ], N - 2 );

}
Else   If (N = 1 )
{
Root. nodes. Add ( " 1 " );
}
Else   If (N = 2 )
{
Root. nodes. Add ( " 1 " );
Root. nodes [ 0 ]. Nodes. Add ( " 1 " );

Root. nodes. Add ( " 2 " );
}
}

After the tree is assembled, the full path of each leaf node is a solution. Therefore, the fullpath (PATH) of the leaf node is printed as a solution. In order to find the leaf node, recursion is also used:

// Print the full path of all leaf nodes
Static   Void Printnode (treenode node)
{
// It belongs to the leaf node !!!! Print the path
If (Node. nodes. Count = 0 )
Console. writeline (node. fullpath );
Else
{
Foreach (Treenode child In Node. nodes)
{
Printnode (child );
}
}
}

When n = 5 is input, the fullpath of all leaf nodes is printed as follows:

Root \ 1 \ 1 \ 1 \ 1 \ 1 \ 1
Root \ 1 \ 1 \ 1 \ 2
Root \ 1 \ 1 \ 2 \ 1
Root \ 1 \ 2 \ 1 \ 1
Root \ 1 \ 2 \ 2
Root \ 2 \ 1 \ 1 \ 1
Root \ 2 \ 1 \ 2
Root \ 2 \ 2 \ 1

We can display a complete solution in a Treeview:

A recursive algorithm is very restricted. It occupies the stack space and increases at the depth level. If the depth is too large, it will quickly reach the upper limit of the space. Therefore, it is only suitable for shallow depth solutions.

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.