跳到正文

个人记录

开黑

xiao_young约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年10月1日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1

这道题其实就是个循环

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

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

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

当每次 abs(0−f(x))<cntabs(0-f(x))<cnt 时 ansans 就是 xx,cntcnt 就是 abs(0−f(x))abs(0-f(x))

AC Code

C++
#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;
}
原文
存档评论1 条抓取于