📄 main.cpp
字号:
#include<iostream.h>
#include<iomanip.h>
#define max 20
main()
{
int n=0;
double x_0=0.0;
double TOL=0.0;
int M=0;
int N=0;
double b[max];
double a[max];
for(int j=0;j<=max;j++)
{
a[j]=0;
b[j]=0;
}
cout<<"Input n"<<endl;
cin>>n;
cout<<"Input x_0"<<endl;
cin>>x_0;
cout<<"Input TOL"<<endl;
cin>>TOL;
cout<<"Input M"<<endl;
cin>>M;
cout<<"Input a_i"<<endl;
for(int i=0;i<=n;i++)
{
cin>>a[i];
}
b[n]=a[n];
double c=a[n];
double x_1=x_0+2*TOL;
while((x_1-x_0)>=TOL||(x_0-x_1)>=TOL)
{
x_0=x_1;
for(int k=n-1;k>=1;k--)
{
b[k]=a[k]+b[k+1]*x_0;
c=b[k]+c*x_0;
}
b[0]=a[0]+b[1]*x_0;
N=N+1;
if(N>M)
{
cout<<"maximum number of iterations exceeded"<<endl;
break;
}
x_1=x_0-b[0]/c;
}
cout<<"Answer:("<<setprecision(10)<<setiosflags(ios::fixed|ios::showpoint)<<x_1<<','<<N<<')'<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -