用递归算法求解f(x)=x-x^3/3!+x^5/5!-x^7/7!+...
YOYO
posted @ 2009年3月10日 20:15
in 【算法】与【数据结构】
with tags
递归
, 3584 阅读
-
#include<iostream>
-
using namespace std;
-
-
double countFx(int x,int p){
-
if(p==0)return 0;
-
if(p==1)return x;
-
float y1 = countFx(x, p-1);
-
float y2 = countFx(x, p-2);
-
-
if(p%2) return y1 + (y2-y1)*(x*x)/((2*p-1)*(2*p-2));
-
-
return y1 - (y1-y2)*(x*x)/((2*p-1)*(2*p-2));
-
}
-
-
int main(){
-
double x;
-
int n;
-
-
cout<<"请输入x和n:";
-
cin>>x>>n;
-
-
cout<<"f("<<x<<")="<<countFx(x,n)<<endl;
-
-
return 0;
-
}