CodeForces987B(思维题,用对数解决)

Description

给你两个数x, y, 比较 x^y 和 y ^ x 的大小

Input

两个数 x, y, ( 1 <= x , y <= 109 )

Output

如果 x ^ y < y ^ x , 输出 “<”

如果 x ^ y > y ^ x , 输出 “>”

如果 x ^ y = y ^ x , 输出 “=”

Sample Input
Input

5 8

Output

>

Input

10 3

Output

<

Input

6 6

Output

=

Hint

第一个例子 5 ^ 8 = 390625, 8 ^ 5 = 32768. 输出 ‘>’.

第二个例子 10 ^ 3 = 1000 < 3 ^10 = 59049.

第三个例子 6 ^ 6 = 46656 = 6 ^ 6.

/*
数据量是10^9 ,不能用快速幂
简单的方法:
如果 x^y <y^x ,则 log(x^y)<log(y^x)(因为 log 函数是递增的)
则 ylog(x) < xlog(y)
所以想到用对数来解决
注意的是:用对数的时候,会出来小数,则需要用 double 型的
10^9 用long long int ,或者是 double

用对数的时候:
c++包含头文件 #include<cmath>
1.double log(double x)  求的是lnx(以 e 为底的对数)
2.double log10(double x) 求 log(10)(x)
求一般的话,假如以a为底的b的对数(log(a)(b))
利用换底公式转化为
lg(b)/lg(a)或 ln(b)/ln(a) 进行求解
*/
代码如下:
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
int main()
{
    double x,y,a,b;
    cin>>x>>y;
    if(x==y)
        cout<<'=';
    else
    {
        double ans,ans1;
        ans=y*log(x);
        ans1=x*log(y);
        if(ans-ans1==0)
            cout<<'=';
        if(ans-ans1<0)
            cout<<'<';
        if(ans-ans1>0)
            cout<<'>';
        cout<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/JKdd123456/article/details/81054844