📜  比较X ^ Y和Y ^ X以获得非常大的X和Y值

📅  最后修改于: 2021-04-23 05:36:37             🧑  作者: Mango

给定两个整数XY ,任务是比较X YY X以获得较大的XY值。

例子:

天真的方法:一种基本方法是找到值X YY X并比较它们,因为X和Y的值可能很大,这可能会溢出

更好的方法:取两个方程的对数, log(X Y )= Y * log(X)log(Y X )= X * log(Y) 。现在,可以轻松比较这些值而不会发生溢出。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to compare x^y and y^x
void compareVal(int x, int y)
{
  
    // Storing values OF x^y AND y^x
    long double a = y * log(x);
    long double b = x * log(y);
  
    // Comparing values
    if (a > b)
        cout << x << "^" << y << " > "
             << y << "^" << x;
  
    else if (a < b)
        cout << x << "^" << y << " < "
             << y << "^" << x;
  
    else if (a == b)
        cout << x << "^" << y << " = "
             << y << "^" << x;
}
  
// Driver code
int main()
{
    long double x = 4, y = 5;
  
    compareVal(x, y);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
      
// Function to compare x^y and y^x
static void compareVal(int x, int y)
{
  
    // Storing values OF x^y AND y^x
    double a = y * Math.log(x);
    double b = x * Math.log(y);
  
    // Comparing values
    if (a > b)
        System.out.print(x + "^" + y + " > " + 
                         y + "^" + x);
  
    else if (a < b)
        System.out.print(x + "^" + y + " < " +
                         y + "^" + x);
  
    else if (a == b)
        System.out.print(x + "^" + y + " = " +
                         y + "^" + x );
}
  
// Driver code
public static void main(String[] args) 
{
    int x = 4, y = 5;
  
    compareVal(x, y);
}
} 
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
from math import log
  
# Function to compare x^y and y^x 
def compareVal(x, y) :
      
    # Storing values OF x^y AND y^x
    a = y * log(x);
    b = x * log(y);
      
    # Comparing values
    if (a > b) :
        print(x, "^", y, ">", y, "^", x);
          
    elif (a < b) :
        print(x, "^", y, "<", y ,"^", x);
  
    elif (a == b) :
        print(x, "^", y, "=", y, "^", x); 
  
# Driver code 
if __name__ == "__main__" : 
  
    x = 4; y = 5; 
  
    compareVal(x, y); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
class GFG
{
      
// Function to compare x^y and y^x
static void compareVal(double x, double y)
{
  
    // Storing values OF x^y AND y^x
    double a = y * Math.Log(x);
    double b = x * Math.Log(y);
  
    // Comparing values
    if (a > b)
        Console.Write (x + "^" + y + " > " + 
                       y + "^" + x);
  
    else if (a < b)
            Console.Write (x + "^" + y + " < "+
                           y + "^" + x);
  
    else if (a == b)
        Console.Write (x + "^" + y + " = " +
                       y + "^" + x );
}
  
// Driver code
static public void Main ()
{
    double x = 4, y = 5;
  
    compareVal(x, y);
}
}
  
// This Code is contributed by ajit.


输出:
4^5 > 5^4