Problem F: 多项式求和x+(x^2)/2!+(x^3)/3!+...

Description

输入一个实数x,计算多项式x+(x2)/2!+(x3)/3!+…的和,直到末项的绝对值小于0.00001(保留三位小数)

Input

输入一个实数x

Output

输出多项式的和, 保留三位小数。

Sample Input

1
2
3.2

Sample Output

1.718
6.389
23.533

本人所写的源程序如下:

#include<stdio.h>
#include<math.h>
int main(){
	double factorial(int);
	int n=1;
	double x,s=0;
	scanf("%lf",&x);
	while(pow(x,n)/factorial(n)>=1e-5){
		s=s+pow(x,n)/factorial(n);
		n++;
	}
	printf("%.3lf\n",s);
}
double factorial(int a){//定义一个求阶乘的方法 
	double s=1;
	for(int i=1;i<=a;i++){
		s=s*i;
	}
	return s;
}

要点在于阶乘函数、while语句以及int型、float型变量以及double型的把握上。

猜你喜欢

转载自blog.csdn.net/AFEI666666/article/details/86500329