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.