C + + to judge odd and even instances introduction _c language

Source: Internet
Author: User
So far, the recursive functions that are seen are directly calling themselves. Although most recursive functions conform to this form, the definition of recursion is more extensive, and if a function is subdivided into several child functions, a recursive call can be applied at a deeper nesting level. For example, if function f calls function g, and function g in turn calls function F, the invocation of these functions is still considered recursive. This type of recursion is becoming an interactive recursive
The following shows the application of interactive recursion by determining whether a number is even or odd, and this topic highlights the importance of the trust of recursive jumps first of all, look at the odd and even-numbered descriptions:
If the first number of a number is odd, then the number is an even
A tree is either an even number or an odd number
definition 0 is an even
Trust of Recursive jumps
As you can see from the code, the implementation of the code is based entirely on the odd and even-numbered three points above. At first glance, this is so incredible. If you want to explore how the bottom is implemented, you can only use a small number of generation, trace call validation is OK
Simply from the surface, the simple scenario of "defining 0 is even" really doesn't see how this recursion works correctly. So, for the inability to see this situation, we need is recursive leap of trust, as long as we recursive decomposition of the correct and simple scenario analysis is correct, implementation details do not have to worry about, to the computer. And so, as long as we have a recursive mind, how simple and quick to solve a problem, how shocking
Copy Code code 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 (one) << 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.