I forgor💀

This commit is contained in:
juan 2021-11-02 10:50:36 +08:00
parent 235484859e
commit d4ef5f38b9
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7

49
pset6/5-exp.c Normal file
View file

@ -0,0 +1,49 @@
#include <math.h>
#include <stdio.h>
// Function prototypes
double expe(double x);
double my_pow(double x, double l);
double get_sing(double x, int l);
int main(void) {
// Initial variables
double x;
scanf("%lf", &x);
printf("%.4lf\n", expe(x));
return 0;
}
double expe(double x) {
double ans = 0;
double addup;
for (int i = 0;; i++) {
addup = (get_sing(x, i));
if (fabs(addup) < 1e-8) {
break;
}
ans += addup;
}
return ans;
}
double my_pow(double x, double l) {
double ans = 1;
for (long long i = 0; i < l; i++) {
ans *= x;
}
return ans;
}
double get_sing(double x, int l) {
if (l <= 0) {
return 1;
} else {
double ans = my_pow(x, l);
for (int i = l; i > 1; i--) {
ans /= i;
}
return ans;
}
}