试试我的程序吧,直接贴到某个地方就能用,的代码显示看起来不美观,凑合看吧,建议贴到程序里看方便一些,已经调试通过:#includeusingnamespacestd;constfloatEPS=0.000001;//定义常量floatffloat;//定义函数ffloatffloatx{returnx*x-5*x+2;}voidmain{floata,b,x1,x2,f1,f2,t,eps;cout>a;cout>b;cout>eps;x1=a+0.382*b-a;x2=a+0.618*b-a;f1=fx1;f2=fx2;whileb-a>eps//搜索精度循环节{t=f1-f2;ift>EPS{a=x1;x1=x2;f1=f2;x2=a+0.618*b-a;f2=fx2;}elseift>=-EPS&&t<=EPS{a=x1;b=x2;x1=a+0.382*b-a;x2=a+0.618*b-a;f1=fx1;f2=fx2;}//函数值相等,两边区间均舍去else{b=x2;x2=x1;f2=f1;x1=a+0.382*b-a;f1=fx1;}}cout<<"x*="<
黄睿刚2019-12-21 18:11:03