外贸企业网站制作哪家好,c2c代表网站,iis7重启 网站,长沙市政务服务中心官网题目#xff1a;
对一个非负整数n来说#xff0c;它的阶乘可以写成 n! (读作“n的阶乘”)#xff0c;其计算公式定义如下#xff1a; n! n x (n-1) x (n-2)x......x1#xff08;对于大于1的 n #xff09; 和 n! 1 ( 对于等于0或者等于1的n )
例如#xff0c;5
对一个非负整数n来说它的阶乘可以写成 n! (读作“n的阶乘”)其计算公式定义如下 n! n x (n-1) x (n-2)x......x1对于大于1的 n 和 n! 1 ( 对于等于0或者等于1的n )
例如55 x 4 x 3 x 2 x 1,结果是120.下面用while语句完成
a)编写一个程序要求读入一个非负整数然后计算和打印它的阶乘
b)编写一个程序使用如下公式 e11/1!1/2!……1/(n−1)!
c)编写一个程序使用如下的公式估算e^x的值
e1x/1!(x^2)/(2!)……( x^(n-1) )/( (n−1)! )
程序如下
a)编写一个程序要求读入一个非负整数然后计算和打印它的阶乘
//4.34a.cpp#include iostream
using namespace std;int main()
{unsigned int num;cout 请输入一个非负整数;cin num;double jiecheng 1; // 存放阶乘的变量if (num 0 || num 1){jiecheng 1;}else{while (num ! 0){jiecheng * num;num--;}}cout 它的阶乘是 jiecheng endl;return 0;
}
运行截图
b)编写一个程序使用如下公式
e11/1!1/2!……1/(n−1)!
//4.34b.cpp#include iostream
#include iomanip
using namespace std;int main()
{int i 0, sumNumber, num;double e 1;cout e得精度是:;cin sumNumber; // 累加求和的项数也是精度if (sumNumber 1) // 控制当精度为1时e1{i 1;}while (i sumNumber){num i; // 用于计算阶乘的数double jiecheng 1; // 存放阶乘的变量if (i 0){jiecheng 1; // 此时e1break;}else{while (num ! 0){jiecheng * num;num--;}}e (1 / jiecheng);cout setprecision(6) fixed;i; // 控制循环}cout e e endl;return 0;
}
运行截图精度为1和2为特殊情况 注意标准e2.718281828459045 c)编写一个程序使用如下的公式估算e^x的值
e^x1x/1!(x^2)/(2!)……( x^(n-1) )/( (n−1)! )
//4.34c.cpp#include iostream
#include iomanip
#include cmath
using namespace std;int main()
{int i 0, sumNumber, num;double e 1;int x; // x表示的e的幂数cout 请输出e的幂数;cin x;cout e^ x 次幂要求的精度是:;cin sumNumber; // 累加求和的项数也是精度if (sumNumber 1) // 控制当精度为1时e1{i 1;}while (i sumNumber){num i;double jiecheng 1; // 存放阶乘的变量if (i 0){jiecheng 1; // 此时e1break;}else{while (num ! 0){jiecheng * num;num--;}}e (pow(x, i) * (1 / jiecheng));cout setprecision(6) fixed;i; // 控制循环}cout e e endl;return 0;
}
运行截图满足特殊情况精度为1
注意百度e²≈7.3891