Dados tres enteros a, b y n, la tarea es encontrar el mayor valor entre a n y b n .
Ejemplos:
Entrada: a = 3, b = 4, n = 5
Salida: b^n es mayor que a^n El
valor de a n es 243 y el valor de b n es 1024.
Entonces, b n es mayor que a n .
Entrada: a = -3, b = 2, n = 4
Salida: a^n es mayor que b^n El
valor de a n es 243 y el valor de b n es 16.
Entonces, a n es mayor que b n .
Enfoque básico: para cada valor de a, b y n, calcule los valores de a n y b n . A continuación, compare el resultado obtenido y visualice el resultado según la salida.
El problema con este enfoque surge cuando hay valores grandes de a, b y n. Para valores grandes de a, n, el cálculo de una n puede exceder el límite de enteros, lo que provocará un desbordamiento de enteros.
Un mejor enfoque es comprobar el valor de n.
- Si n es par, calcule el valor absoluto de a y b.
- Si n es impar, tome el valor dado tal como es.
- Ahora comprueba si a es igual a b. En caso afirmativo, imprima 0.
- Si a es mayor que b, imprima 1.
- De lo contrario, imprime 2.
C++
// C++ code for finding greater // between the a^n and b^n #include <bits/stdc++.h> 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
<?php // PHP code for finding greater // between the a^n and b^n // Function to find // the greater value function findGreater($a, $b, $n) { // If n is even if (!($n & 1)) { $a = abs($a); $b = abs($b); } if ($a == $b) echo "a^n is equal to b^n"; else if ($a > $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 ?>
Javascript
<script> // Javascript code for finding greater // between the a^n and b^n // Function to find // the greater value function findGreater(a , b , n) { // If n is even if (!((n & 1) > 0)) { a = Math.abs(a); b = Math.abs(b); } if (a == b) document.write("a^n is " + "equal to b^n"); else if (a > b) document.write("a^n is greater " + "than b^n"); else document.write("b^n is greater " + "than a^n"); } // Driver code var a = 12, b = 24, n = 5; findGreater(a, b, n); // This code is contributed by todaysgaurav </script>
b^n is greater than a^n
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por RohanDeoli y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA