Poj1664-put apple (recursion), poj1664-put apple Recursion

Source: Internet
Author: User

Poj1664-put apple (recursion), poj1664-put apple Recursion


I. Question:
M apples are placed on N plates, allowing empty plates and asking how many different methods are there.
Ii. Ideas:
Recursive solution:
1. There is a process of repeated execution (call itself)
In the first case, n> m: there must be n-m plates empty, which does not affect the removal.
Case 2 n <= m:
I. At least one plate is empty;
Ii. Each plate has an apple;
The total number of methods for Apple is the sum of the two:
2. There are conditions for jumping out of the repeated execution process (recursive Exit)
When an Apple has only one or more plates
* Recursion:
I, n will gradually decrease and eventually reach the exit n = 1;
Ii, m gradually decreases, because when n> m, return work (m, m) will eventually reach the exit m = 0;
Step 3:
1, if (n> m) work (m, n) = work (m, m );
Else
I, work (m, n) = work (m, n-1 );
Ii, work (m, n) = work (m-n, n );
Work (m, n) = work (m, n-1) + work (m-n, n );
2, if (m = 0 | n = 1) return 1;

1 # include <iostream> 2 using namespace std; 3 4 int work (int m, int n) {5 if (m = 0 | n = 1) 6 return 1; 7 if (n> m) 8 return work (m, m); 9 else10 return work (m, n-1) + work (m-n, n ); 11} 12 13 int main () {14 int t, m, n; 15 cin> t; 16 while (t --) {17 cin> m> n; 18 cout <work (m, n) <endl; 19} 20 return 0; 21}View Code

Note:
1. recursion is to call itself in a process or function.
2. When using a recursive policy, there must be a clear recursive termination condition called the recursive exit.
3. recursive algorithms are usually simple to solve, but the efficiency of recursive algorithms is low. Therefore, recursive algorithms are generally not recommended for programming.
4. During the recursive call process, the system opens a stack for storing the return points and local volumes of each layer. Too many recursion times may cause stack overflow. Therefore, recursive algorithms are generally not recommended for programming.
Requirements:
1. Each call is scaled down (usually halved );
2. There is a close relationship between two adjacent duplicates. The previous one should be prepared for the next one (the previous output is usually used as the next input );
3. When the scale of the question is extremely small, you must provide a direct answer without making a recursive call. Therefore, each recursive call is conditional (the scale is not the size of the direct answer ), unconditional recursive call will become an endless loop and cannot end normally.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

 

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.