1213. Massive Numbers

 

The number is called massive if it is represented in the form  a^n, which means a raised in power n. You need to compare two massive numbers ab and cd, written in the form “<base>^<exponent>”.

Given two massive numbers, print the biggest one.

 

Input. Two massive numbers a and b in the form “<base>^<exponent>”. It is known that 1 ≤ <base>, <exponent> ≤ 1000.

 

Output. The biggest number among a and b.

 

Sample input

Sample output

3^100 2^150

3^100

 

 

SOLUTION

mathematics

 

Algorithm analysis

Take the logarithm on both sides of an inequality ab < cd and get b lg a < d lg c. The values of expressions b lg a and d lg c fit into the double type, so to compare them is not difficult.

 

Algorithm realization

Read the input data. Separate the base and exponent for each number.

 

scanf("%d^%d %d^%d",&a,&b,&c,&d);

 

Compare the logarithms of expressions b lg a and d 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 realization

 

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);

  }

}

 

Java realization – Scanner, delimiter

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in).useDelimiter("\\s|\\^");

    //System.out.println(con.delimiter());   

    int a = con.nextInt();

    int b = con.nextInt();

    int c = con.nextInt();

    int d = con.nextInt();

    if(b * Math.log(a) > d * Math.log(c))

      System.out.println(a + "^" + b);

    else

      System.out.println(c + "^" + d);

    con.close();

  }

}