Home > Back-end >  Recursive solution
Recursive solution

Time:11-23

[img=https://img-bbs.csdn.net/upload/202008/24/1598275673_776723.png] [/img

CodePudding user response:

CodePudding user response:

Give you the formula, set the recursive formula is ok

 double fx (double x, double a) {
Double x1;
If (x==0) return 0;//does not exist in theory, in order to rule out anomalies (except 0)
X1=(x + a/x)/2;//formula
If (abs (x1 - x) & lt; 0.00001) return the x1;//to the accuracy of topic request return
Return fx (x1, a);//otherwise continue to recursive (the x1 plug formula continue to ask xn)
}

Int main (void)
{
Double a;
Lf the scanf (" % ", & amp; a);
Double x=fx (1 a);//x0=1
Printf (" % 0 lf \ n ", x);//the topic request is rounded part
return 0;
}

CodePudding user response:

"Given a bit of input, a complete single-step tracking (and press Alt + 7 key to view the Call Stack from the inside to the following out of from the inner to outer function Call history) again," is your least-hassle route to understand the working principle of the recursive function!
Recursive function pay attention to the following factors
Exit criteria,
What parameters,
What, the return value is
What are local variables,
What are global variables,
When output,
, will not lead to stack overflow
  • Related