Math Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

Problem Description
Here has an function:
  f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R)
Please figure out the maximum result of f(x).
 
Input
Multiple test cases(less than 100). For each test case, there will be only 1 line contains 6 numbers a, b, c, d, L and R. (−10≤a,b,c,d≤10,−100≤L≤R≤100)
 
Output
For each test case, print the answer that was rounded to 2 digits after decimal point in 1 line.
 
Sample Input
1.00 2.00 3.00 4.00 5.00 6.00
 
Sample Output
310.00
 
 
代码:
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; // f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R) int main()
{
double a, b, c, d, ll, r;
double mm, dd, ff;
double gg, hh; while(cin>>a)
{
cin>>b>>c>>d>>ll>>r; dd=(a*pow(ll, 3.0)+b*pow(ll, 2.0)+c*ll+d);
if(dd<0)
dd=-dd;
ff=(a*pow(r, 3.0)+b*pow(r, 2.0)+c*r+d);
if(ff<0)
ff=-ff;
mm=max(dd, ff); if((4*b*b - 12*a*c)<0)
{
//无解
printf("%.2lf\n", mm);
continue;
}
else if( (4*b*b - 12*a*c)==0 )
{
//有一个解
if( (-(b)/(3*a))>=ll && (-(b)/(3*a))<=r )
{
gg=-1*(b/3*a);
hh=a*pow(gg,3)+b*pow(gg, 2)+c*gg+d;
if(hh<0)
hh=-hh;
if(hh>mm)
mm=hh;
printf("%.2lf\n", mm);
continue;
}
}
else
{
//有2个解
gg=(-b+sqrt(b*b-4*a*c))/2*a;
if( gg>=ll && gg<=r )
{
double q;
q=a*pow(gg,3)+b*pow(gg, 2)+c*gg+d;
if(q<0)
q=-q;
if(q>mm)
mm=q;
} hh=(-b*sqrt(b*b-4*a*c))/2*a;
if(hh>=ll && hh<=r)
{
double w;
w=a*pow(hh,3)+b*pow(hh, 2)+c*hh+d;
if(w<0)
w=-w;
if(w>mm)
mm=w;
} printf("%.2lf\n", mm);
}
} return 0;
}

Bestcoder round 18----B题(一元三次方程确定区间的最大值(包含极值比较))的更多相关文章

  1. BestCoder Round #1 第一题 逃生

    // 等了好久,BESTCODER 终于出来了..像咋这样的毕业的人..就是去凑凑热闹// 弱校搞acm真是难,不过还是怪自己不够努力// 第一题是明显的拓扑排序,加了了个字典序限制而已// 用优先队 ...

  2. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  3. BestCoder Round #1 第二题 项目管理

    // 第二题 我记得很久很久很久以前看过这样的题目,忘记是哪的区域赛了 // 记得有人说和节点度数有关,我记不清了,反正当时完全不懂 // 然后我想了想,估计就是更新节点度数有关,YY出来可能只要更新 ...

  4. BestCoder Round #18(hdu5105)Math Problem(高中数学)

    最大值无非就是在两个端点或极值点处取得. 我注意讨论了a=0和b=0,却忽略了极值点可能不在L到R的范围内这一问题.被Hack了. #include<iostream> #include& ...

  5. ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)

    Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he ...

  6. 从lca到树链剖分 bestcoder round#45 1003

    bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ...

  7. BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元

    BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy  Init函数 然后统计就ok B. 博弈 题  不懂  推了半天的SG.....  结果这 ...

  8. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  9. BestCoder Round #86 二,三题题解(尺取法)

    第一题太水,跳过了. NanoApe Loves Sequence题目描述:退役狗 NanoApe 滚回去学文化课啦! 在数学课上,NanoApe 心痒痒又玩起了数列.他在纸上随便写了一个长度为 nn ...

随机推荐

  1. 初级Springboot(一)

    初级Springboot(一) 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 一.了解Springboot 做Java开发的小伙伴都知道,我们在做项目的时候,需要去写大量的配置文件 ...

  2. Network | DHCP

    动态主机设置协议(Dynamic Host Configuration Protocol, DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途: 给内部网络或网络服务供应商自动分配I ...

  3. Jenkins自动化部署入门(一)

    开始使用 Jenkins 这一段时间,技术总监为了减少测试环境每次提交新增接口都要部署项目的时间,搞了一个jenkins持续集成github.docker,这样只要每次push代码都会自动部署,确实节 ...

  4. UITableView 滚动时使用reloaddata出现 crash'-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)' Crash

    例子: - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)in ...

  5. python中 urllib, urllib2, httplib, httplib2 几个库的区别

    转载 摘要: 只用 python3, 只用 urllib 若只使用python3.X, 下面可以不看了, 记住有个urllib的库就行了 python2.X 有这些库名可用: urllib, urll ...

  6. Java并发学习 & Executor学习 & 异常逃逸 & 同步互斥Best Practice & wait/notify, conditon#await/signal

    看了这篇文章:http://www.ciaoshen.com/2016/10/28/tij4-21/ 有一些Java并发的内容,另外查了一些资料. 朴素的Thread 首先,Java中关于线程Thre ...

  7. Hibernate调试——定位查询源头

    本文是我在importNew翻译的文章,首发在importNew,这里会定期更新链接. 为什么有时Hibernate会在程序某一部分生成一条指定sql查询?这个问题让人非常难立马理解.当处理不是我们本 ...

  8. 【Python】输出程序运行的百分比

    对于一些大型的Python程序.我们须要在命令行输出其百分比,显得更加友好,以免被人误会程序陷入死循环.假死的窗口. 关键是利用到不换行的输出符\r,\r的输出.将直接覆盖掉此行的内容. 比方例如以下 ...

  9. source insight的查找功能

    source insight是一款非常好的c语言的程序编辑器.方便对project管理,方便程序的阅读和编辑. 查找功能使用十分频繁.选项较多,与其他软件的查找功能也类似,以下对英文版的查找功能,做简 ...

  10. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...