#include<iostream> #include<cmath> using namespace std; int jc(int n) { int c; int a = 1; for (c = 1; c <= n; ++c) { a = a * c; } return a; } int main() { int x, y, c; double sum = 1; double z=0; cin >> x; for ( y = 1; abs (z )>= 1e-8;++y) { c= jc(y); z = pow(x, y) * pow(-1, y + 1) / c; sum = sum + z; } cout << sum; }
#include<iostream> #include<cmath> using namespace std; long jc(int n); long jc(int n) { int c; long a = 1; for (c = 2; c <= n; ++c) a = a * c; return a; } int main() { long x, y, c; double sum = 1; double z = 1; cin >> x; for (y = 1; abs(z) >= 1e-8; ++y) { c = jc(y); z = pow(x, y) * pow(-1, y + 1) / c; sum = sum + z; } cout << sum << endl; } --来自助手版贴吧客户端