【HDU】5105 Math Problem

来源:互联网 发布:seo招聘北京 编辑:程序博客网 时间:2024/04/30 21:41

传送门:【HDU】5105 Math Problem


题目分析:如果a不等于0,说明是一元三次方程,求个导取极值的坐标以及两端点的坐标,带入f(x)求个最大值就好。如果a等于0,那么如果b不等于0,则是一元二次方程,直接得到极值坐标,和两端点的坐标带入f(x)中求个最大值。如果b也等于0,那么ans = max (f(L),f(R))。

注意极值坐标是否存在以及是否可行。


代码如下:


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std ;typedef long long LL ;#pragma comment ( linker , "/STACK:1024000000" )#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )#define rec( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] )#define clr( a , x ) memset ( a , x , sizeof a )double a , b , c , d , L , R ;double f ( double x ) {return fabs ( a * x * x * x + b * x * x + c * x + d ) ;}void solve () {double x1 , x2 ;double ans = max ( f ( L ) , f ( R ) ) ;if ( a ) {double sqr = 4 * b * b - 12 * a * c ;if ( sqr < 0 ) x1 = x2 = 10000 ;else {x1 = ( - 2 * b - sqrt ( sqr ) ) / ( 6 * a ) ;x2 = ( - 2 * b + sqrt ( sqr ) ) / ( 6 * a ) ;}if ( L <= x1 && x1 <= R ) ans = max ( ans , f ( x1 ) ) ;if ( L <= x2 && x2 <= R ) ans = max ( ans , f ( x2 ) ) ;} else if ( b ) {double x = - c / 2 / b ;if ( L <= x && x <= R ) ans = max ( ans , f ( x ) ) ;}printf ( "%.2f\n" , ans ) ;}int main () {while ( ~scanf ( "%lf%lf%lf%lf%lf%lf" , &a , &b , &c , &d , &L , &R ) ) solve () ;return 0 ;}


0 0
原创粉丝点击