C 函式數值問題
撰寫一個程式計算 e 的近似值 , 並印出前 1~10 項近似值。 (e =1 + 1/1! + 1/2! + 1/3! + 1/4! + …)
1 個解答
評分
- 北海牧羊人Lv 47 年前最佳解答
#include <stdio.h>
#include <stdlib.h>
int f(int n){ //計算n!的遞迴函式
return (n>1)?n*f(n-1):1;
}
int main(){
double e=1.0;
int i;
for(i=1;i<10;i++) //做到第十項
e+=(1.0/((double)f(i)));
printf("e approximate to 10th elements is %lf\n",e);
system("pause");
return 0;
}
參考資料: 每天和程式作伍的研究生
還有問題?馬上發問,尋求解答。