题目描述
用递归方法求n阶勒让德多项式的值,递归公式为
n=0 p
n(x) =1
n=1 p
n(x) =x
n>1 p
n(x) =((2n-1)*x* p
n-1(x) -(n-1)* p
n-2(x))/n
结果保留2位小数。
输入
n和x的值。
输出
pn(x)的值。
样例输入
2 2
样例输出
5.50
#include<stdio.h>
int main()
{
float n,x,s;
float lerangde(float ,float );
scanf("%f%f",&n,&x);
s=lerangde(n,x);
printf("%.2f",s);
return 0;
}
float lerangde(float n,float x)
{
float f;
if(n==0)f=1;
else if(n==1)f=x;
else
f=((2*n-1)*x*lerangde(n-1,x)-(n-1)*lerangde(n-2,x))/n;
return(f);
}