📄 (1)(2).cpp
字号:
#include<iostream.h>
float fun1(float N)
{
float Sn=0,i;
for(i=2;i<=N;i++)
{Sn=Sn+1/(i*i-1);}
return Sn;
}
float fun2(float N)
{
float Sn=0,i;
for(i=N;i>=2;i--)
{Sn=Sn+1/(i*i-1);}
return Sn;
}
float fun3(float N)
{
float Sn;
Sn=1.0/2.0*(3.0/2.0-1/N-1/(N+1));
return Sn;
}
void main()
{
float N;
cout<<"Please input N:";
cin>>N;
cout<<"按第一种算法得Sn为:"<<fun1(N)<<endl;
cout<<"按第二种算法得Sn为:"<<fun2(N)<<endl;
cout<<"精确值为:"<<fun3(N)<<endl;
}
/*程序运行结果为:
Please input N:100
按第一种算法得Sn为:0.740049
按第二种算法得Sn为:0.74005
精确值为:0.740049
Please input N:10000
按第一种算法得Sn为:0.749852
按第二种算法得Sn为:0.7499
精确值为:0.7499
Please input N:1000000
按第一种算法得Sn为:0.749852
按第二种算法得Sn为:0.749999
精确值为:0.749999
Press any key to continue*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -