首页 > ACM题库 > HDU-杭电 > hdu 2675 Equation Again-分治-[解题报告]C++
2014
02-12

hdu 2675 Equation Again-分治-[解题报告]C++

Equation Again

问题描述 :

This problem’s author is too lazy to write the problem description, so he only give you a equation like X(eY) == (eY)x, and the value of Y, your task is calculate the value of X.
Note : here e is the Natural logarithm.

输入:

Each line will contain one number Y(Y >= 1). Process to end of file.

输出:

Each line will contain one number Y(Y >= 1). Process to end of file.

样例输入:

1

样例输出:

2.71828

/*
取对数后得
ln(x)/x=(1+ln(y))/(e*y)
显然右边是常数
作图可知道ln(x)/x在x>=1范围先增后减,极值在x=e处
对于任意一个y!=1,都有左右两个解,二分解决就是了……
*/
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double ee=2.718281828459;
const double eps=1e-7;
int main()
{
    double x,y;
    // for(int i=1;i<=2000;i++)
    // cout<<log(i)/(1.0*i)<<endl;
    // while(scanf("%lf",&y)!=EOF)
    while(scanf("%lf",&y)!=EOF)
    {
        double tmp=(1+log(y))/(y*ee);
        double low=1+eps,hei=ee-eps,mid;
        if(tmp*ee-1>eps){puts("Happy to Women’s day!");continue;}
        while(hei-low>eps)
        {
            mid=(hei+low)/2;
            if(log(mid)*(y*ee)>(1+log(y))*mid)
            hei=mid;
            else 
            low=mid;
        }
        if(y==1)printf("%.5lf\n",(hei+low)/2);
        else  if(y>1)
        {
            double ans=(hei+low)/2;
            low=ee+eps,hei=2000000000+eps;
            while(hei-low>eps)
            {
                mid=(hei+low)/2;
                if(log(mid)*(y*ee)<(1+log(y))*mid)
                hei=mid;
                else 
                low=mid;
            }
            printf("%.5lf %.5lf\n",ans,(hei+low)/2);
        }
    }
    return 0;
}

解题转自:http://blog.csdn.net/wsniyufang/article/details/6687142


  1. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同,其树的前、中、后序遍历是相同的,但在此处不能使用中序遍历,因为,中序遍历的结果就是排序的结果。经在九度测试,运行时间90ms,比楼主的要快。

  2. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!