#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
double x;
while (cin >> x) {
double sum = 1,N = 1 ,d = 1 ;
int flag = 1;
while (1.00 /d * pow(x, 1.00 * N) >= 1e-6)
{
sum += 1.0*flag / d * pow(x, 1.00*N);
flag = -flag;
N++;
d *= N;
}
cout << fixed << setprecision(6) << "x="
<< x << ", " << "sum=" << sum
<< endl;
}
}
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double x, fz;
while (cin >> x) {
double sum = 1, fh = 1, h = 1;
int flag = 1;
for (int i = 1;; i++) {
fz = pow(x, 1.00 * i);
h *= i;
fh = fz / h * flag;
sum += fh;
flag = -flag;
if (fh < 1e-6 && fh > -(1e-6)) {
break;
}
}
cout << fixed << setprecision(6) ;
cout << "x=" << x << ",sum=" << sum << "\n";
}
return 0;
}
实在看不懂这两段为什么第一段通过了第二段没通过,用devc++输出两者都是正确值,有没有朋友指点一下