Introduction to c ++'s evaluation of odd and even numbers
Source: Internet
Author: User
So far, we have seen recursive functions that call ourselves directly. Although most recursive functions comply with this form, recursion is more widely defined. If a function is subdivided into several subfunctions, you can apply recursive calls at a deeper nesting level. For example, if function f calls function g and function g calls function f in turn, these functions are still called as recursion. This type
Recursion becomes interactive Recursion The following example shows the application of interaction recursion by judging whether a number is an even number or an odd number. This topic highlights the importance of trust in recursive jump. First, let's first look at the description of odd and even numbers:
If the previous number of a number is an odd number, the number is an even number.
A tree is an odd number if it is not an even number.
Define 0 as an even number Recursive jump Trust
From the code, we can see that the implementation of the code is completely based on the three points described in the preceding odd and even numbers. At first glance, this is incredible. If you want to explore how it is implemented at the underlying layer, you only need to use a small number to import the data.
For example, simply looking at the surface, the simple scenario of "defining 0 as an even number" cannot tell whether this recursion works correctly. Therefore, what we need for such a situation that cannot be seen at once is the trust of recursive jump. As long as the recursive decomposition is correct and the simple scenario analysis is correct, we don't have to worry about the implementation details, hand it over to the computer. Therefore, as long as you master recursive thinking, how simple, fast, and shocking to solve a problem
Copy codeThe Code is as follows: # include <iostream>
Using namespace std;
Bool isodd (unsigned );
Bool isodd (unsigned n)
{
Return! (Iseven (n ));
}
Bool iseven (unsigned n)
{
If (n = 0)
{
Return true;
}
Else
{
Return isodd (n-1 );
}
}
Int main ()
{
Cout <isodd (11) <endl;
Return 0;
}
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.