Time
Limit: 1
Sec Memory Limit: 128 MB
Submit: 161 Solved: 105
[Submit][Status][Web
Board]
Description
用递归方法求n阶勒让德多项式的值,递归公式为
n=0
pn(x)
=1
pn(x)
=1
n=1 pn(x)
=x
=x
n>1 pn(x)
=((2n-1)*x* pn-1(x)
-(n-1)* pn-2(x))/n
=((2n-1)*x* pn-1(x)
-(n-1)* pn-2(x))/n
结果保留2位小数。
Input
n和x的值。
Output
pn(x)的值。
Sample Input
2 2
Sample Output
5.50
#includeiostream>
#includeiomanip>
#includecmath>
using namespace std;
double polya(int n,double x)
{double y;
if(n==0)
return 1;
else if(n==1) return x;
else
{
y=((2*n-1)*x* polya(n-1,x) -(n-1)* polya(n-2,x))/n;
return y;
}
}
int main()
{
int x,n;
cin>>n>>x;
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
cout<<polya(n,x)<<endl;
return 0;
}