📜  程序在a ^ n和b ^ n之间找到更大的值

📅  最后修改于: 2021-04-29 03:01:42             🧑  作者: Mango

给定三个整数a,b和n,任务是找出a n和b n之间的更大值。

例子:

基本方法:对于a,b和n的每个值,计算a n和b n的值。然后比较获得的结果并显示相应的输出。

当a,b和n的值很大时,就会出现这种方法的问题。对于较大的a,n值,计算n可能会超过整数的限制,这将导致整数溢出。

更好的方法是检查n的值。

  • 如果n为偶数,则计算a和b的绝对值。
  • 如果n为奇数,则按原样使用给定值。
  • 现在检查a是否等于b。如果是,则打印0。
  • 如果a大于b,则打印1。
  • 否则,请打印2。
C++
// C++ code for finding greater
// between the a^n and b^n
#include 
using namespace std;
  
// Function to find the greater value
void findGreater(int a, int b, int n)
{
    // If n is even
    if (!(n & 1)) {
  
        a = abs(a);
        b = abs(b);
    }
    if (a == b)
        cout << "a^n is equal to b^n";
  
    else if (a > b)
        cout << "a^n is greater than b^n";
  
    else
        cout << "b^n is greater than a^n";
}
  
// Driver code
int main()
{
    int a = 12, b = 24, n = 5;
    findGreater(a, b, n);
    return 0;
}


Java
// JAVA code for finding greater
// between the a^n and b^n
import java.io.*;
  
class GFG
{
    // Function to find
    // the greater value
    static void findGreater(int a, 
                            int b, int n)
{
    // If n is even
    if (!((n & 1) > 0)) 
    {
  
        a = Math.abs(a);
        b = Math.abs(b);
    }
    if (a == b)
        System.out.println("a^n is " + 
                           "equal to b^n");
  
    else if (a > b)
        System.out.println("a^n is greater " + 
                                  "than b^n");
  
    else
        System.out.println("b^n is greater " + 
                                  "than a^n");
}
  
// Driver code
public static void main (String[] args) 
{
int a = 12, b = 24, n = 5;
findGreater(a, b, n);
}
}
  
// This code is contributed
// by shiv_bhakt.


Python3
# Python3 code for finding greater
# between the a^n and b^n
import math
  
# Function to find the greater value
def findGreater(a, b, n):
  
    # If n is even
    if ((n & 1) > 0):
  
        a = abs(a);
        b = abs(b);
    if (a == b):
        print("a^n is equal to b^n");
  
    elif (a > b):
        print("a^n is greater than b^n");
  
    else:
        print("b^n is greater than a^n");
  
# Driver code
a = 12; 
b = 24; 
n = 5;
findGreater(a, b, n);
  
# This code is contributed by mits


C#
// C# code for finding greater
// between the a^n and b^n
using System;
  
class GFG
{
    // Function to find
    // the greater value
    static void findGreater(int a, 
                            int b, 
                            int n)
    {
    // If n is even
    if (!((n & 1) > 0)) 
    {
  
        a = Math.Abs(a);
        b = Math.Abs(b);
    }
      
    if (a == b)
        Console.WriteLine("a^n is " + 
                          "equal to b^n");
  
    else if (a > b)
        Console.WriteLine("a^n is greater " + 
                                 "than b^n");
  
    else
        Console.WriteLine("b^n is greater " + 
                                 "than a^n");
}
  
// Driver code
public static void Main() 
{
    int a = 12, b = 24, n = 5;
    findGreater(a, b, n);
}
}
  
// This code is contributed
// by shiv_bhakt.


PHP
 $b)
        echo "a^n is greater than b^n";
  
    else
        echo "b^n is greater than a^n";
}
  
// Driver code
$a = 12; $b = 24; $n = 5;
findGreater($a, $b, $n);
  
// This code is contributed by ajit
?>


输出:
b^n is greater than a^n