2023C++电子 0224038 雷可
例6
//计算e=1 + 1/1! + 1/2!+ 1/3! +...当1/n!<1e-7时停止
#include<iostream>
using namespace std;
#include"cmath"
int main()
{
double e = 1.0;
double u = 1.0;
int n = 1;
while (u >= 1.0e-7)
{
u = u / n;
e = e + u;
n = n + 1;
}
cout << "e=" << e << "(n=" << n << ")" << endl;
return 0;
}
例7
//计算三位数的水仙花数(个,十,百位数的立方和等于该数本身)
#include <iostream>
using namespace std;
int main()
{
int n, i, j, k;
for (n = 100; n < 1000; n++)
{
i = n / 100;
j = (n / 10) % 10;
k = n % 10;
if (n == i * i * i + j * j * j + k * k * k)
cout << n << "=" << i << "^3+" << k << "^3" << endl;
}
return 0;
}