使用函数求e的近似值分数 8作者 颜晖单位 浙大城市学院自然常数e可以用级数1+11!+12!+⋯+1n!来近似计算。本题要求实现一个计算阶乘的简单函数使得可以利用该函数对给定的非负整数n求该级数的前n+1项和。函数接口定义:double fact int n ;其中n是用户传入的参数函数返回n的阶乘。裁判测试程序样例:#include stdiohdouble fact int n ;int m
#include <stdio.h>
double fact( int n ) { double result = 1; // 注意要使用double类型 for (int i = 1; i <= n; i++) { result *= i; } return result; }
int main(void)
{
int i, n;
double sum;
scanf("%d", &n);
sum = 1;
for(i = 1; i <= n; i++){
sum = sum + 1.0/fact(i);
}
printf("%f\n", sum);
return 0;
}
原文地址: http://www.cveoy.top/t/topic/fmA0 著作权归作者所有。请勿转载和采集!