hdu 2899 Strange fuction

来源:互联网 发布:认知心理学 知乎 编辑:程序博客网 时间:2024/05/01 18:10
Strange fuctionTime Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
SubmitStatusPracticeHDU 2899

Description

Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.

Input

The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)

Output

Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

Sample Input

2100200

Sample Output

-74.4291-178.8534


解题报告:一道三分法的题,直接套三分法模板


code:

#include<iostream>#include<algorithm>#include<cstdio>#include<queue>#include<stack>#include<math.h>#include<string>#include<cstring>using namespace std;typedef long long ll;const double eps=1e-10;double f(double x,double y){    return 6*pow(x,7)+8*pow(x,6)+7*x*x*x+5*x*x-y*x;}void bs(double y){    double l=0,r=100.0;    double mid,mmid,k=0;    while(l+eps<r){        mid=(l+r)/2;        mmid=(l+mid)/2;        if(f(mid,y)<f(mmid,y))            l=mmid;        else{            r=mid;            k=r;        }    }    printf("%.4lf\n",f(k,y));}int main(){   // freopen("input.txt","r",stdin);    int t;    double y;    scanf("%d",&t);    while(t--){        scanf("%lf",&y);        bs(y);    }}



这道题也可以用二分法做:

解题报告:这个题是个简单的数学题求最值。F''在0<x<100大于0。所以 F'(x)单调递增,存在一点x0,在(0,x0)递减,(x0,100)递增,F(x0)为最小值。所以这道题就变成了用二分法找x0;code:
    #include <iostream>      #include<algorithm>      #include<math.h>      #include<stdio.h>      using namespace std;            const double eps=1e-10;      double y;            double f(double x){          return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;      }      double ff(double x){          return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x;      }            double bs(){          double left=0,right=100,mid;          while(left+eps<=right){              mid=(left+right)/2;              if(f(mid)<0)                  left=mid;              else                  right=mid;          }          return mid;      }            int main(){        //freopen("input.txt","r",stdin);         int t;         double x;         scanf("%d",&t);         while(t--){            scanf("%lf",&y);            x=bs();            printf("%.4f\n",ff(x));         }            return 0;      }  




0 0
原创粉丝点击