AC助手说的最优解
2024-05-19 20:03:16
发布于:广东
65阅读
0回复
0点赞
#include<bits/stdc++.h>//最优解
using namespace std;
double factorial(int n) {
double fact = 1;
for (int i = 1; i <= n; ++i) fact *= i;
return fact;
}
int main() {
int n;
double f = 1;
cin >> n;
for (int i = 1; i <= n; ++i) {
double inv_factorial = 1.0 / factorial(i);
if (i % 2 == 1) f -= inv_factorial;
else f += inv_factorial;
}
cout << fixed << setprecision(8) << f << endl;
return 0;
}
这里空空如也
有帮助,赞一个