hdu2675 二分查找

题目

给你一个Y,要你求出 X(eY) == (eY)x 的解.

思路: 先化简得 ln(x)/x == ln(ey)/ey,再二分查找,注意解有一个或两个,需要画图

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
#define e 2.718281828459
#define eps1 1e-10
#define eps 1e-6
#define INF 0x7fffffff
const int MAXN=1000000000;
const int MOD=10000000;

int check(double x,double y){
	if(x/log(x) >= e*y/log(e*y))return 0;
	return 1;
}

int main(){
	SIS;
	double y;
	while(~scanf("%lf",&y)){
		double l=1,r=e,mid;
		while(r-l>eps1){
			mid=l+(r-l)/2;
			if(check(mid,y))r=mid;
			else l=mid;
		}
		double ans1=l;
		l=e;r=INF;
		while(r-l>eps1){
			mid=l+(r-l)/2;
			if(check(mid,y))l=mid;
			else r=mid;
		}
		double ans2=l;
		if(ans2-ans1>=eps){
			printf("%.5lf %.5lf\n",ans1,ans2);
		}
		else 
		printf("%.5lf\n",ans1);
	}
    return 0;
}
发布了20 篇原创文章 · 获赞 2 · 访问量 249

猜你喜欢

转载自blog.csdn.net/weixin_45535964/article/details/105013253