HDU 2899: Strange fuction

本文展示了一种使用C++实现的一维搜索算法,通过计算特定函数F(x)的值来寻找最佳解。该算法利用了二分查找的思想,并考虑到了导数的影响,最终输出接近最优解的函数值。

Strange fuction


#include<bits/stdc++.h>
using namespace std;
const double eps=1.1e-5;
inline double F(const double &x,const double &y )
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}
inline double get_derivative(const double & x,const double &y)
{
return 42*pow(x,6) + 48*pow(x,5) + 21*pow(x,2) + 10*x - y;
}

inline bool my_binary_search(double & _low,double & _high,const double & y)
{
while(_high-_low>=eps)
{
long double mid;
mid = (_low + _high) / 2;
if(get_derivative(mid,y)<0)
_low=mid;
else _high=mid;
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int y;
cin>>y;
double low=0,high=100;
my_binary_search(low,high,y);
cout<<fixed<<setprecision(4)<<F(low,y)<<'\n';
}
return 0;
}

转载于:https://www.cnblogs.com/zjnu/p/7372501.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值