📜  程序比较m ^ n和n ^ m

📅  最后修改于: 2021-05-30 14:20:43             🧑  作者: Mango

给定两个正整数mn ,任务是编写一个程序,检查m ^ n是否大于,小于或等于n ^ m。

例子 :

天真的方法是计算m ^ n和n ^ m,这会在m和n非常大时导致溢出。

一种有效的方法是使用log解决此问题。

C++
// CPP program to compare which is greater 
// m^n or n^m  
#include 
using namespace std;
  
// function to compare m^n and n^m 
void check(unsigned long long m, unsigned long long int n)
    {
        // m^n 
        double RHS = m * (double)log(n);
          
        // n^m 
        double LHS = n * (double)log(m); 
          
        if ( LHS > RHS )
            cout << "m^n > n^m";
              
        else if ( LHS < RHS )
            cout << "m^n < n^m";
              
        else
            cout << "m^n = n^m";
    }
  
// Drivers Code 
int main() {
      
    unsigned long long m = 987654321, n = 123456987;
      
    // function call to compare m^n and n^m
    check(m, n);
      
    return 0;
}


Java
// Java program to compare which 
// is greater m^n or n^m 
import java .io.*;
  
class GFG
{
// function to compare
// m^n and n^m 
static void check(long m, long n)
{
    // m^n 
    double RHS = m * (double)Math.log(n);
      
    // n^m 
    double LHS = n * (double)Math.log(m); 
      
    if (LHS > RHS)
        System.out.print("m^n > n^m");
          
    else if (LHS < RHS)
    System.out.print("m^n < n^m");
          
    else
        System.out.print("m^n = n^m");
}
  
// Driver Code 
static public void main (String[] args)
{
    long m = 987654321, n = 123456987;
  
    // function call to 
    // compare m^n and n^m
    check(m, n);
}
}
  
// This code is contributed by anuj_67.


Python3
# Python3 program to compare 
# which is greater m^n or n^m 
import math
  
# function to compare
# m^n and n^m 
def check( m, n):
      
    # m^n 
    RHS = m * math.log(n);
      
    # n^m 
    LHS = n * math.log(m); 
      
    if (LHS > RHS):
        print("m^n > n^m");
          
    elif (LHS < RHS):
        print("m^n < n^m");
          
    else:
        print("m^n = n^m");
  
# Driver Code 
m = 987654321; 
n = 123456987;
  
# function call to 
# compare m^n and n^m
check(m, n);
  
# This code is contributed by mits


C#
// C# program to compare which  
// is greater m^n or n^m 
using System;
  
class GFG
{
// function to compare
// m^n and n^m 
static void check(ulong m, ulong n)
{
    // m^n 
    double RHS = m * (double)Math.Log(n);
      
    // n^m 
    double LHS = n * (double)Math.Log(m); 
      
    if (LHS > RHS)
        Console.Write("m^n > n^m");
          
    else if (LHS < RHS)
    Console.Write("m^n < n^m");
          
    else
        Console.Write("m^n = n^m");
}
  
// Driver Code 
static public void Main ()
{
    ulong m = 987654321, n = 123456987;
  
    // function call to 
    // compare m^n and n^m
    check(m, n);
  
}
}
  
// This code is contributed by anuj_67.


PHP
 $RHS )
        echo "m^n > n^m";
          
    else if ( $LHS < $RHS )
    echo "m^n < n^m";
          
    else
        echo "m^n = n^m";
}
  
// Driver Code 
$m = 987654321; 
$n = 123456987;
  
// function call to 
// compare m^n and n^m
check($m, $n);
  
// This code is contributed by anuj_67.
?>


输出 :

m^n < n^m