zl程序教程

您现在的位置是:首页 >  后端

当前栏目

习题 4.9 用递归方法求n阶勒让德多项式的值,递归公式为:

方法递归 习题 公式 多项式 4.9
2023-09-14 09:06:56 时间

C++程序设计(第三版) 谭浩强 习题4.9 个人设计

习题 4.9 用递归方法求n阶勒让德多项式的值,递归公式为:KaTeX parse error: Undefined control sequence: \mbox at position 24: …begin{cases}1 &\̲m̲b̲o̲x̲(n = 0)\\x &\mb…

代码块:

#include <iostream>
#include <iomanip>
using namespace std;
double p(int n, int x);                  //定义求值函数
int main()
{
    double r;
    int s, y;
	cout<<"Please enter n, x: ";
	cin>>s>>y;
    r=p(s, y);                           //调用求值函数
	cout<<"Result: "<<setiosflags(ios::fixed)<<setprecision(4)<<r<<endl;
	system("pause");
    return 0;
}
//求值函数
double p(int n, int x)
{
    if (n==0)
        return 1;
    else if (n==1)
        return x;
    else
        return ((2*n-1)*x-p(n-1, x)-(n-1)*p(n-2, x))/n;
}