ACM simulation questions (4) -- Recursion

Source: Internet
Author: User

Recursion is very intuitive to solve some problems, but you should pay attention to the depth of recursion when using recursion. If the depth is too deep, Stack Overflow may occur. The following example shows how to use it.

Question: Super stair
Problem Description
There is a total of M-level stairs. At the beginning, you are at the first level. If you can only cross the upper or second level at a time, how many steps should you go to the level M?
Input
The input data first contains an integer N, indicating the number of test instances, and then N rows of data. Each row contains an integer M (1 <= M <= 40), indicating the level of the stairs.
Output
For each test instance, please output the number of different steps
Sample Input
2
2
3
Sample Output
1
2
 
Solution: first, we need to find the rules for these questions. We can try to write a few M cases, and then find the rules from them. For example:
M walk
1
2 1-2
3 1-3 (two levels from 1) 1-2-3 (one level from 2)
4 1-2-4 (two levels from 2) 1-2-3-4 1-3-4 (one level from 3)
5 1-2-3-5 1-3-5 (two steps from 3) 1-2-3-4-5 1-2-4-5 1-3-4-5 (level from 4)
6 1-2-4-6 1-2-3-4-6 1-3-4-6 (two steps from 4)
1-2-3-5-6 1-3-5-6 1-2-3-4-5-6 1-2-4-5-6 1-3- 4-5-6 (from 5 to 5)
...
All the steps of n N-2 layer can reach n layer in two levels, and all the steps of n-1 layer can reach n layer
N layer walk can be expressed as f (n), then f (n) = f (n-1) + f (n-2), n> 4
In this way, Recursive Computing can be used. The reference code is as follows:
/*
* Super stair
*/
Public static int test7 (int n ){
Switch (n ){
Case 1: return 0;
Case 2: return 1;
Case 3: return 2;
Default: return test7 (n-1) + test7 (n-2 );
}
}
For such a question, recursion is not fast, and it will overflow if recursion is too deep. You can use a loop to process it. refer to the following code:
/*
* Super stair
*/
Public static int test7 (int n ){
Int [] result = new int [40];
Result [0] = 0;
Result [1] = 1;
Result [2] = 2;
For (int I = 3; I <n; I ++ ){
Result [I] = result [I-1] + result [I-2];
}
Return result [n-1];
}
Li xucheng CSDN Blog: http://blog.csdn.net/javaeeteacher
CSDN student base camp: http://student.csdn.net/space.php? Uid = 1, 124362
If you like my article, add me as a friend: http://student.csdn.net/invite.php? U= 124362 & c = 7be8ba2b6f3b6cc5

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.