遞歸求P函數
任務描述
本關任務:編寫函數 funP,完成數學函數P(n,x)P(n,x)函數的計算,定義如下:
測試樣例
測試輸入:12 2.34
預期輸出:P(12, 2.34)=5.1583
測試輸入:9 9.8
預期輸出:P(9, 9.8)=25.8949
源代碼
#include <iostream>
using namespace std;
// 函數funP:實現數學函數P函數
// 返回值:返回P(n,x)的值
double funP(int n, double x)
{
// 請在這里補充代碼,實現遞歸函數funP
/********** Begin *********/
if(n==0){
return 1;
}
if(n==1){
return x;
}
if(n>1){
return ((2*n-1)*funP(n-1,x)-(n-1)*funP(n-2,x))/n;
}
/********** End **********/
}
int main()
{
int n;
double x;
cin >> n >> x; // 輸入n、x
cout << "P("<<n<<", "<<x<<")=" << funP(n,x) << endl;
return 0;
}