[C++] 用递归方法求 n 阶勒让德多项式的值 →→→→→进入此内容的聊天室

来自 , 2020-07-26, 写在 C++, 查看 163 次.
URL http://www.code666.cn/view/008bd5ad
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.         int x,n;
  6.         cout<<"Please enter the value of the \"x\":\n";
  7.         cin>>x;
  8.         cout<<"Please enter the value of the \"n\":\n";
  9.         cin>>n;
  10.        
  11.         double P(int x,int n);
  12.         if(n==0){
  13.                 cout<<"P(x)="<<1;
  14.         }
  15.         else if(n==1){
  16.                 cout<<"P(x)="<<x;
  17.         }
  18.         else{
  19.                 cout<<"P(x)="<<P(x,n);
  20.         }
  21.         return 0;
  22. }
  23.  
  24. double P(int x,int n){
  25.         double result = 0.0;
  26.         for(n;n>1;n--){
  27.                 result = ((2*n-1)*x-P(x,n-1)-(n-1)*P(x,n-2))/n;
  28.         }
  29.         return result;
  30. }
  31.  

回复 " 用递归方法求 n 阶勒让德多项式的值"

这儿你可以回复上面这条便签

captcha