欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

hdu 2199 Can you solve this equation? 二分

发布时间:2023/11/27 58 豆豆
生活随笔 收集整理的这篇文章主要介绍了 hdu 2199 Can you solve this equation? 二分 小编觉得挺不错的,现在分享给大家,帮大家做个参考.



1.精度问题 由于是double类型,r=mid 而不是r=mid-1

2.如果首位两端(f(0)和f(100))同号,证明解不在[1,100]区间内 这是我之所以TE的原因,没有预先判断

3.若在这个区间内,则一定可要求出解 所以binarysearch 返回m


#include <time.h>
#include <iomanip>
#include <cstdlib>
#include <stdio.h>
#define eps 1e-6
using namespace std;
double y;
double f(double x)
{return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6-y;
}
double binaryserch(double l, double r)
{double m;while((r-l)>=eps){m=(l+r)/2;if(f(m)>0) r=m;else l=m;}return m;}
int main()
{int num;cin>>num;while(num--){cin>>y;if(f(0)*f(100)>0) cout<<"No solution!"<<endl;else printf("%.4lf\n",binaryserch(0,100));}return 0;
}

转载于:https://www.cnblogs.com/LandingGuy/p/9280294.html

总结

以上是生活随笔为你收集整理的hdu 2199 Can you solve this equation? 二分的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。