20200802--利用公式 e=1+1/1!+1/2!+...+1/n!,求e的值, 要求保留小数点后10位(奥赛一本通 p67 2)

//第一种解法,单for,关键是t*=i这行

# include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  double e,s;
  e=1.0;
  s=0.0;
  long long t=1;
  scanf("%d",&n);
for(int i=1;i<=n;i++)
  {t*=i;//关键步骤,它存储了以前的结果
  e+=1.0/t;
  }
printf("%.10lf",e);
return 0;
}

//第二种解法,两层for循环,内for循环完后,t=1

# include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  double e,s;
  e=1.0;
  s=0.0;
  long long t=1;
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    for(int ls=1;ls<=i;ls++){
      t*=ls;
    }
  e+=1.0/t;
  t=1;
  }
  printf("%.10lf",e);
  return 0;
}

原文地址:https://www.cnblogs.com/whcsrj/p/13418873.html