首页 > 代码库 > HDU 5105 Math Problem(数学)
HDU 5105 Math Problem(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105
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).
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
Source
BestCoder Round #18
PS:
分a等于零 和a不等于零的情况分别求导讨论!
代码如下:
#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> #include <cstring> using namespace std; int main() { double a, b, c, d, L, R; while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R)) { double ansL = 0, ansR = 0; double maxans = 0; if(a != 0) { double t = 4*b*b-12*a*c; ansL = a*L*L*L+b*L*L+c*L+d; if(ansL < 0) ansL = -ansL; ansR = a*R*R*R+b*R*R+c*R+d; if(ansR < 0) ansR = -ansR; maxans = max(ansL,ansR); if(t >= 0) { double x1 = (-2*b+sqrt(t))/(6*a); double t1 = a*x1*x1*x1+b*x1*x1+c*x1+d; if(t1 < 0) t1 = -t1; maxans = max(maxans,t1); double x2 = (-2*b-sqrt(t))/(6*a); double t2 = a*x2*x2*x2+b*x2*x2+c*x2+d; if(t2 < 0) t2 = -t2; maxans = max(maxans,t2); } } else if(a == 0) { if(b != 0) { double tt = -c/(2*b); double t1 = a*tt*tt*tt+b*tt*tt+c*tt+d; if(t1 < 0) t1 = -t1; maxans = t1; } double t1 = a*L*L*L+b*L*L+c*L+d; if(t1 < 0) t1 = -t1; maxans = max(maxans,t1); double t2 = a*R*R*R+b*R*R+c*R+d; if(t2 < 0) t2 = -t2; maxans = max(maxans,t2); } printf("%.2lf\n",maxans); } return 0; }
HDU 5105 Math Problem(数学)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。