Judge whether a number is an even number or an odd number.

Source: Internet
Author: User

Interaction Recursion
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 of recursion becomes interactive Recursion

The following code uses an even number or an odd number to demonstrate the application of recursive interaction. This question highlights the importance of trust in recursive jump.

First, first look at the description of the 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

[Cpp]
# 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;
}

 

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.