有的课本如c++程序设计基础教程中公式不一样放到P2中
P1:
#include<iomanip>
#include<iostream>
using namespace std;
double p(int x,int n);
int main()
{
int x,n;
cout<<"请输入x值和其阶数:"<<endl;
cin>>x>>n;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<"p"<<x<<"("<<n<<")"<<"="<<p(x,n);
}
double p(int x,int n)
{
if (n==0)
{
return 1;
}
else if (n==1)
{
return x;
}
else
{
return ((2*n-1)*x*p(x,n-1)-(n-1)*p(x,n-2))/n;
}
}
P2:
#include<iomanip>
#include<iostream>
using namespace std;
double p(int x,int n);
int main()
{
int x,n;
cout<<"请输入x值和其阶数:"<<endl;
cin>>x>>n;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<"p"<<x<<"("<<n<<")"<<"="<<p(x,n);
}
double p(int x,int n)
{
if (n==0)
{
return 1;
}
else if (n==1)
{
return x;
}
else
{
return ((2*n-1)*x-p(x,n-1)-(n-1)*p(x,n-2))/n;
}
}