A logarithm is a monotonically increasing function, so if x < y, then log(x) < log(y).
Let's take the logarithm of the inequality: a^b < c^d. For example, using the common logarithm (or natural logarithm, it doesn’t matter):
Therefore, comparing two large numbers reduces to comparing the two values b \cdot lg a and d \cdot lg c. The values of b \cdot lg a and d \cdot lg c fit into a type, so they can be easily compared.
Algorithm implementation
Read the input data. Extract the bases and exponents for each number.
scanf("%d^%d %d^%d",&a,&b,&c,&d);Compare the values of the logarithmized expressions b \cdot lg a and d \cdot lg c. Then print the answer.
if (b * log(1.0 * a) < d * log(1.0 * c)) printf("%d^%d\n",c,d);
else printf("%d^%d\n",a,b);Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
String s = con.nextLine();
StringTokenizer st = new StringTokenizer(s," ^");
int a = Integer.valueOf(st.nextToken()),
b = Integer.valueOf(st.nextToken()),
c = Integer.valueOf(st.nextToken()),
d = Integer.valueOf(st.nextToken());
if(b * Math.log(a) > d * Math.log(c))
System.out.println(a + "^" + b);
else
System.out.println(c + "^" + d);
con.close();
}
}