惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
博客园 - 聂微东
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
S
SegmentFault 最新的问题
F
Fortinet All Blogs
H
Help Net Security
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
L
LangChain Blog
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
AOJ 482.二分法求方程的根|OhYee 博客
2017-03-18 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

{% raw %}

{% endraw %} 用二分法求方程f(x)=ax4+bx3+cx2+dx+e=0 在 (x1,x2) 之内的根 本题保证f(x1)*f(x2)<=0且(x1,x2)内只有一个根

{% raw %}


{% endraw %}

行1:5个空格分隔的整数a,b,c,d,e,a,b,c,d,e∈[-9,9]
行2:2个空格分隔的整数x1,x2,x1,x2∈[-10,10],x1<x2

{% raw %}


{% endraw %}

行1:一个浮点数,代表根,精确到小数点后10位

{% raw %}




{% endraw %}

0 2 -4 3 -6
-10 10

{% raw %}


{% endraw %}

2.0000000000

{% raw %}




{% endraw %}

题解

由于最后要输出 10 位,因此 eps 至少要是 1e-11
然后就是模拟数学运算即可

为了防止错误,建议全部使用 double
数学函数部分可以写成一个函数或者宏定义,简化代码

没什么要注意的细节,知道数学计算思路就行

代码

```cpp 二分法求方程的根 https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份 //*/ #define debug #include

//*/ #include #include #include #include #include using namespace std;

const double eps = 1e-12;

#define f(x) axxxx + bxxx+cxx+dx+e

bool equal(double a,double b) {
return fabs(a - b) < eps;
}

int main(){
#ifdef debug
freopen("in.txt", "r", stdin);
int START = clock();
#endif
cin.tie(0);
cin.sync_with_stdio(false);

double a,b,c,d,e,x1,x2;
while(cin >> a >> b >> c >> d >> e >> x1 >> x2){
    double x = (x1+x2)/2;
    double y1 = f(x1);
    double y2 = f(x2);
    //cout << "y1 = "<<y1<<" y2 = "<<y2<<endl;
    while(1){
        x = (x1+x2)/2;
        double y = f(x);
        //cout << "x = "<<x<<" y = "<<y<<endl;
        getchar();
        if(equal(0.0,y))
            break;
        if(y*y1 < 0)
            x2 = x;
        else
            x1 = x;
    }
    cout << fixed << setprecision(10) << x << endl;
}

#ifdef debug
printf("Time:%.3fs.\n", double(clock() - START) / CLOCKS_PER_SEC);
#endif
return 0;

}

</div>