zoukankan      html  css  js  c++  java
  • Bestcoder round 18----B题(一元三次方程确定区间的最大值(包含极值比较))

    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)=|ax3+bx2+cx+d|(LxR)
    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. (10a,b,c,d10,100LR100)
     
    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
    ", 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
    ", 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
    ", mm);
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    123. Best Time to Buy and Sell Stock III (Array; DP)
    122. Best Time to Buy and Sell Stock II (Array;Greedy)
    121. Best Time to Buy and Sell Stock (Array;DP)
    38. Count and Say (String; DP)
    60. Permutation Sequence (String; Math)
    内存中的堆栈
    42. Trapping Rain Water (Array,stack; DP)
    84. Largest Rectangle in Histogram (Array, Stack; DP)
    85. Maximal Rectangle (Graph; Stack, DP)
    Effective C++ .44 typename和class的不同
  • 原文地址:https://www.cnblogs.com/yspworld/p/4100206.html
Copyright © 2011-2022 走看看