几条最简单的编程题

来源:百度知道 编辑:UC知道 时间:2024/05/28 17:47:45
1。求1!+2!+3!+.....+n!,n是20以内的数
2。用公式e=1+1/1!+1/2!+1/3!+……+1/n!,求e的近似值,直到1/n!<10e-6。
3。要实现10年国民生产总值翻两翻的目标,问每年的增长值为多少?(精确到x.xx%)
c语言的

1.
#include <stdio.h>
main()
{
float n,s=0,t=1;
for(n=1;n<=20;n++)
{
t=t*n;
s=s+t;
}
printf("1!+2!+…+20!=%e\n",s);
}

2.会1就不用我说2了吧。
3.第三题就是个初中问题,也不用说了吧?!

3

1.
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main()
{
int i,n;
double ans,now;
cin>>n;
ans=0; now=1;
for (i=1;i<=n;i++)
{
now*=i;
ans+=now;
}
cout<<ans<<endl;
return 0;
}

2.
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main()
{
int n;
double ans,now;
ans=1; now=1; n=0;
while (1)
{
n++;
now*=n;
ans+=1/now;
if (1/now<10e-6) break;
}
cout<&l