#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int x, n;
long power, factorial;
double series, subitem;
short sign = -1;
subitem = 0.0;
series = 0.0;
cout << "Please input number x:";
cin >> x;
cout << "Please input number n:";
cin >> n;
for (int itemN = 0; itemN <= n; itemN++) {
//Get the n-th Power of number x.
for (int powerJ = 0; powerJ <= itemN; powerJ++) {
if (powerJ == 0) {
power = 1;
continue;
}
power *= x;
}
//Get the factorial of number n.
for (int factorialK = 0; factorialK <= itemN; factorialK++) {
if (factorialK == 0 || factorialK == 1) {
factorial = 1;
continue;
}
factorial *= factorialK;
}
if (itemN == 0 || itemN == 1) {
subitem = (double)power / factorial;
}
else {
subitem = (double)power / factorial;
subitem *= -1;
}
series += subitem;
}
cout << fixed << setprecision(8) << series << endl;
return 0;
}
C++程序设计教程第二版钱能 2.9练习2第1题
最新推荐文章于 2023-08-16 15:21:43 发布