HDU 5105

寒假训练七:F题

传送门:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=107154#problem/F

F - F
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Here has an function: 
  $f(x) = |a * x^3 + b * x^2 + c * x + d| (L \leq x \leq 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 \leq a, b, c, d \leq 10, -100 \leq L \leq R \leq 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

题意,给出a,b,c,d,l,r,求在[l,r]里f(x)=|ax^3+bx^2+cx+d|的最大值


数学问题,分类讨论a,b是否为0,再求导,一元二次方程求根即可


交了三次才过,一次忘了有绝对值,一次忘了判断b^2-4ac的大小

虽说简单但讨论的内容不能漏

下面代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<functional>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;

double a,b,c,d,l,r;
	

double f(double x)
{
	return fabs(a*x*x*x+b*x*x+c*x+d);
}

double ff(double x)
{
	return 3*a*x*x+2*b*x+c;
}


int main()
{
	double ans1,ans2,dmax;
	double ll,rr,mid1,mid2;
	
	while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&l,&r))
	{
		ans1=max(f(l),f(r));
		if(a==0 && b==0) dmax=ans1;
		else if(a==0 && -c/(2*b)<=r && -c/(2*b)>=l) dmax=max(ans1,f(-c/(2*b)));
		else if(a==0) dmax=ans1;
		else if(a!=0) 
		{
			ans2=0;
			if(2*b*2*b-4*c*3*a>=0)
			{
				mid1=(-2*b-sqrt(2*b*2*b-4*c*3*a))/(6*a);
				mid2=(-2*b+sqrt(2*b*2*b-4*c*3*a))/(6*a);
				if(mid1<=r && mid1>=l) ans2=f(mid1);
				if(mid2<=r && mid2>=l && f(mid2)>ans2) ans2=f(mid2);
				dmax=max(ans1,ans2);
			}
			else dmax=ans1;
		} 
		printf("%.2lf\n",dmax);
	}
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值