#include <stdio.h> #include <math.h> double cal_e(int n){ double sum=1.0, term = 1.0; int i; for (i=1; i<=n; i++){ term = term * 1.0 / i; //计算当前项的值 sum += term; //将当前项加到总和中 if (fabs(term) < 1e-16) break; //如果当前项的绝对值已经小于 1e-16,则退出循环 } return sum; } int main(){ int n; double sum; if (scanf("%d", &n) != EOF){ sum = cal_e(n); printf("%.8f", sum); } else { printf("input n wrong"); } return 0; }