比较 X^Y 和 Y^X 非常大的 x 和 y 值

原文:https://www . geeksforgeeks . org/comparison-xy-and-yx-for-x-and-y 的超大值/

给定两个整数 XY ,任务是比较XYT7】和YXT11】的 XY 的大值。 举例:****

输入: X = 2,y = 3 T3】输出:2^3<3^2 23t18】32T10输入: X = 4,y = 5 t14】输出: 4^5 > 5^4

天真的方法:一个基本的方法是找到值XYT5】和YXT9】并比较它们,因为 X 和 Y 的值可能很大 更好的方法:取两个方程的对数,log(XY)= Y * log(X)log(YX【T11 现在,这些值可以很容易地进行比较,而不会溢出。 以下是上述办法的实施:****

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

Python 3

# 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.

java 描述语言

<script>
// Javascript implementation of the approach

// Function to compare x^y and y^x
function compareVal(x, y)
{

    // Storing values OF x^y AND y^x
    let a = y * Math.log(x);
    let b = x * Math.log(y);

    // Comparing values
    if (a > b)
        document.write(x + "^" + y + " > "
             + y + "^" + x);

    else if (a < b)
        document.write(x + "^" + y + " < "
             + y + "^" + x);

    else if (a == b)
        document.write(x + "^" + y + " = "
             + y + "^" + x);
}

// Driver code
    let x = 4, y = 5;

    compareVal(x, y);

</script>

Output: 

4^5 > 5^4