HDU-2899 Strange fuction
求
fx=6x7+8x6+7x3+5x2−yx(0<=x<=100) 的最小值。
題目很簡單,三分就好了,但三分很坑啊。
注意
1.三等分點公式
mid1=2head+tail3
mid2=head+2tail3
2.注意刪除調試語句啊233。
代碼
#include<bits/stdc++.h>
using namespace std;
void work();
double cf(double x,double u)
{
double ans=1;
for(int i=1;i<=u;i++)
ans*=x;
return ans;
}
double fx(double x,double y)
{
//cout<<x<<" "<<y<<endl;
double ans=1;
ans=6*cf(x,7)+8*cf(x,6)+7*cf(x,3)+5*cf(x,2)-y*cf(x,1);
return ans;
}
int main()
{
int T;
cin>>T;
for(int i=1;i<=T;i++)
work();
}
void work()
{
double y;
cin>>y;
double tail,mid1,mid2,head;
head=-0.00001;tail=100.1;
while(tail-head>0.000001)
{
mid1=(2*head+tail)/3;
mid2=(head+2*tail)/3;
//cout<<mid1<<" "<<y<<endl;
double a1=fx(mid1,y),a2=fx(mid2,y);
//cout<<a1<<" "<<a2<<endl;
//system("pause");
if(a1>a2)
head=mid1;
else tail=mid2;
}
printf("%0.4f\n",fx(head,y));
}