遞歸求P函數


遞歸求P函數

任務描述

本關任務:編寫函數 funP,完成數學函數P(n,x)P(n,x)函數的計算,定義如下:

1586768207126

測試樣例

測試輸入: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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM