Strange fuction
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 14 Accepted Submission(s) : 12
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem 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
2 100 200
Sample Output
-74.4291 -178.8534
这题..
首先double类型的判断大小不能直接用==这些来判断 可以define一个eps为1e-6
#define eps 1e-6
if(fabs(a-b)<eps)//判断是否相等
这题好像有两种方法 暂时只写出一种
要求原函数的最小值 就是导函数为0的时候 原函数的二阶导恒大于0 所以导函数是单调递增的 某种方面可以说他是有序的一个序列 那就可以用二分查找 要绕过导函数为0 把算式转换一下就是 我下面写的ff()要等于y
ac代码:
#include<iostream>
#include<cmath>
#define eps 1e-6
double y;
double f(double x)//原函数
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-x*y;
}
double ff(double x)//导函数
{
return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}
int main()
{
using namespace std;
int T,N;
cin>>T;
while(T--)
{
cin>>y;
double l=0,r=100,mid;
while(l+eps<r)
{
mid=(l+r)/2;
if(ff(mid)<y)
l=mid;
else
r=mid;
}
printf("%.4lf\n",f(mid));
}
return 0;
}
再放下老师的三分查找吧
/*hdu2899 三分
题意:求函数F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)的最小值,y为给定的实数,x连续实数且在[0,100]范围内
思路:函数F(x)的一阶导数F'为先负后正,二阶导数F''>0即一阶导数单调,由此可得F(x)为单峰凹函数,因此可用三分来解。
*/
#include<stdio.h>
double y;
double f(double x)
{
return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;
}
//在区间[l,r)范围内查找函数F(x)的凹点(最小值)----三分
double solve(double l, double r)
{
double eps = 1e-7;//一个非常小的常量
while( l+eps <r) //当[l,r]的区间长度非常非常小的时候,我们就认为已经找到答案
{
double lmid = l +(r-l)/3, rmid = r-(r-l)/3;
if( f(lmid) <f(rmid) )//查找目标在左半区(2/3),把右半区(1/3)丢掉
r = rmid;//目标一定在[l,rmid)
else//查找目标在右半区(2/3),把左半区(1/3)丢掉
l = lmid;//目标一定在[lmid,r)
}
return f(l);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lf",&y);
printf("%.4lf\n", solve(0,100.0) );
}
}