PTA: recursive computation function P (15 minutes) (C language)

The title computing required to achieve the following function P (n, x), which function is defined as follows:
Here Insert Picture Description
Function interface definition:
Double P (n-int, Double X);

Wherein n is a nonnegative integer incoming user, x is a double precision floating point. P function returns the corresponding value P (n, x) function. Title ensure that the input and output are in double precision.

Referee test sample program:
#include <stdio.h>

double P( int n, double x );

int main()
{
int n;
double x;

scanf("%d %lf", &n, &x);
printf("%.2f\n", P(n,x));

return 0;
}

/ * Your code will be embedded here * /

Sample input:
10 1.7

Sample output:
3.05

double P( int n, double x )
{
    if (n == 0)
        return 1;
    else if (n == 1)
        return x;
    else
        return ((2 * n - 1) * P(n - 1, x) - (n - 1) * P(n - 2, x)) / n;

}
Published 58 original articles · won praise 21 · views 596

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105418709
Recommended