求n阶勒让德多项式

Time
Limit: 1
Sec  Memory Limit: 128 MB
Submit: 161  Solved: 105
[Submit][Status][Web
Board
]

Description

用递归方法求n阶勒让德多项式的值,递归公式为
n=0   
 pn(x)
=1  
n=1     pn(x)
=x
n>1     pn(x)
=((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;
}
上一篇:python的json模块的dumps,loads,dump,load方法介绍


下一篇:红楼梦人物关系图,一代大师成绝响,下回分解待何人,kindle读书摘要