开黑

· · 个人记录

这道题其实就是个循环

题目保留 6 位小数,精度就是 6,只需从 1.200000 遍历到 2.100000 就行了,每次加 0.000001,代码就是这样:

for(double i=1.200000;i<=2.100000;i+=0.000001) 

然后再定义两个变量 ans 和 cnt,分别表示现在最接近 0 的 x 和 0-f(x) 的绝对值

当每次 abs(0-f(x))<cnt 时 ans 就是 x,cnt 就是 abs(0-f(x))

AC Code

#include<bits/stdc++.h>
using namespace std;
double f(double x){
    return pow(x,4)-13*pow(x,5)+87*pow(x,2)-219*x+256*x-125;
}
int main(){
    double ans,cnt=100;
    for(double i=1.200000;i<=2.100000;i+=0.000001){
        double s=f(i);
        if(abs(0-s)<cnt){
            ans=i;
            cnt=abs(0-s);
        }
    }
    cout<<fixed<<setprecision(6)<<ans;
    return 0;
}