7、对运行中输入的x,计算级数:1+ x–x2/2! + x3/3! - …(-1)n+1*xn/n!。
要求输出精度为10-8。
#include
using namespace std;
int main() {
double sum = 1, x, n, a, t = 1, eps = 1e-8;
cout << "请输入x的值:";
cin >> x;
for (n=1.0;x/n>eps ; )
{
a = t * x / n;
sum += a;
n = n * (n +1);
x = x * x;
t *= -1;
};
cout << "sum=" << sum;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
unsigned int jiecheng(unsigned x){
if(x==1){
return 1;
}else{
return x * jiecheng(x-1);
}
}
double fuc(double x ,unsigned int n){
unsigned int i;
float result = 1.0;
if(n==0){
return 1.0;
}
else{
for(i=1;i<=n;i++){
result += pow(-1,i+1)*pow(x,i)/jiecheng(i);
}
return result;
}
}
void main()
{
printf("%lf \n",fuc(1,0));
printf("%lf \n",fuc(1,1));
printf("%lf \n",fuc(2,10));
system("pause");
}