C language function call 17-recursive method call of general functions (2), 17 Recursion

Source: Internet
Author: User

C language function call 17-recursive method call of general functions (2), 17 Recursion
// Recursion
/*
========================================================== ======================================
Question: F (60). F (n) is defined as follows:
F (0) = 0;
F (1) = 1;
F (2n) = f (n) + 3;
F (2n + 1) = F (n) + F (2n-1 ).
========================================================== ======================================
*/
# Include <stdio. h>
Double F (int n)
{
If (n = 0) return 0;
Else if (n = 1) return 1;
Else if (n % 2 = 0) return F (n/2) + 3;
Else if (n % 2! = 0)
Return F (n-1)/2) + F (n-2 );
}
Void main ()
{
Int n;
Float p;
Printf ("n = ");
Scanf ("% d", & n );
P = F (n );
Printf ("F (% d) = %. 2lf \ n", n, p );
}
/*
========================================================== ====================================
Rating:
In the third equation, make t = 2n, so t % 2 = 0, n = t/2;
In the fourth equation, Let t = 2n + 1, so t is an odd number, then n = (t-1)/2, 2n-1 = T-2; (n as t in the Program)
It is easy to compile a recursive relationship!
========================================================== ======================================
*/

Copyright Disclaimer: the author's article can be reprinted in non-commercial use, but please be sure to indicate the source. Due to the limited level, it is inevitable that errors will occur.

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.