問題
代碼
#include <stdio.h>
#include <math.h>
int main() {
double a = 2,y;
long int n=1;
do{
y=a;
n+=1;
a=pow(1+1/(double)n,(double)n);
}while(a-y>1e-6);
printf("e = %f\nn = %ld\n",a,n);
return 0;
}
}
結果
注意
在我寫代碼時,遇到了一個坑。題目中括號跟你講的n爲充分大的正整數,當然是要定義long int n;
那麼問題來了,pow()
函數的原型是:double pow(double x,double y);因此n需要進行強制轉換類型纔可以正確輸出結果。同時
printf()中的格式
轉換爲%ld
。