Autor Tema: Warmup :: Base Comparator ::  (Leído 122 veces)

[D-m-K]

  • Administrador
  • Mensajes: 229
  • [-.|.-]
    • MSN Messenger - d4rk.m0nk3y@hotmail.com
    • Ver Perfil
    • Red Informatica Colombiana
    • Email
Warmup :: Base Comparator ::
« : octubre 14, 2010, 10:08:27 pm »
Bueno aqui traigo otro ejercicio pa cacharrearle un poquito.

Cita
Problem B
Base Comparator


Source file name: bcomp.c, bcomp.cpp or bcomp.java

DigiCircuits Inc. is a software company that develops software simulators for digital circuits. A
very frequently used component of its software, named the comparator, is a simulated circuit
that compares numbers expressed in different numerical bases. More exactly, this component
receives two numbers, each one in a possible different base, and decides if the first number is
less than, equal to or greater than the second number.

The numerical bases that may appear vary from 1 to 9. Remember that a number expressed
in base b uses only digits less than b. Your task is to develop a program that simulates the function of the comparator component.

Input

The input file contains several test cases, each one of them in a separate line. Each test case
has four numerical strings, each two of them separated by a blank character, say
s b t c
Strings b and c are one-character strings. They represent the bases for the first and third
strings s and t, respectively.
The end of the input is denoted by the end of the input file.
The input must be read from the file bcomp.in.

Output

Output text for each input case is presented in the same order that input is read. For each
test case the answer must be a left aligned answer-character corresponding to the input. This
character must be < , = or > , accordingly to the fact that the first string represents a numerical
value less than, equal to or greater that the represented by the third string.
The output must be written to standard output.

<< Esta es la salida que se puede obtener >>


Sample input  Output for the sample input
54 6 71 8  <
110 2 6 7  =
3 4 3 9   =
14 7 1000 2  >



Archivo bcomp.in
Cita
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1 888 9
888 9 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1
000 1 10 2
14 7 8 9
777777777777777777 8 8888888888888888 9
000000000000000000 1 21 8
00000012345678 9 12345678 9
0 1 000000000000000001 5
11111111111111111111111111111 2 2222222222222222222222222222 3
8888888888888888888 9 1111111111111111111111111111111111111111111111111111111111111111111 2
444 5 147 9
000000000 1 10 8


Archivo bcomp.out
Cita
<
>
=
>
>
=
=
<
<
>
=
=

Nota :  Estos ejercicios estan basados en el calentamiento de ACM ICPC / 2006

Pdta : Ahorita pongo el cronometro en marcha y calculo el tiempo ;).

« Última Modificación: octubre 14, 2010, 10:48:27 pm por [D-m-K] »
:: Todas las grandes cosas que se disfrutan son el producto de las pequeñas cosas que se logran ::

[D-m-K]

  • Administrador
  • Mensajes: 229
  • [-.|.-]
    • MSN Messenger - d4rk.m0nk3y@hotmail.com
    • Ver Perfil
    • Red Informatica Colombiana
    • Email
Re: Warmup :: Base Comparator ::
« Respuesta #1 : octubre 14, 2010, 11:41:50 pm »
Aqui esta mi code y mi salida.

bcomp.java
Código: (java) [Seleccionar]
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class bcomp {
    public static void main(String[] args) throws IOException{
        //Objetos de lectura y escritura
        BufferedReader file_in = new BufferedReader(new FileReader("bcomp.in"));
        PrintWriter file_out = new PrintWriter(new FileWriter("bcomp.out"));
        String linea = "";

        //Lectura y proceso de archivo
        while( (linea = file_in.readLine()) != null){
            StringTokenizer st = new StringTokenizer(linea," ");
            BigInteger n1 = new BigInteger(st.nextToken());  //numero1
            int b1 = Integer.parseInt(st.nextToken());       //base1
            BigInteger n2 = new BigInteger(st.nextToken());  //numero2
            int b2 = Integer.parseInt(st.nextToken());       //base2

            //Cambio de bases
            try {   //Excepciones del formato de la base en n1
                n1 = new BigInteger(n1.toString(), b1);
            } catch (NumberFormatException e) {
                n1 = new BigInteger(n1.toString());
            }

            try {//Excepciones del formato de la base en n2
                n2 = new BigInteger(n2.toString(), b2);
            } catch (NumberFormatException e) {
                n2 = new BigInteger(n2.toString());
            }
           
            //System.out.println(n1 + "(" + b1 + ")  : " + n2 + "(" + b2 + ")");
            //Comparo las bases y determino el menor, mayor o igual
            if (n1.compareTo(n2) == 0)
                file_out.println("=");          //Igual
            else if (n1.compareTo(n2) < 0)
                file_out.println("<");          //Menor
            else if (n1.compareTo(n2) > 0)
                file_out.println(">");          //Mayor
        }
        file_out.flush();
        file_out.close();
    }
}

bcomp.out
Cita
<
>
<
>
>
<
=
<
<
<
=
<

Mmmm sera que los de la maraton se equivocaron, porque por mas que revise mi salida no da igual a la planteada y pues si se observan las validaciones logicas no existe una base 1, esto lanza una excepcion del tipo NumbeFormatException :S.

Bueno en tiempo me heche 43 minutos, pues inicialmente no cai en cuenta de usar BigInteger y no sabia como rayos comparar 2 BigInteger :P... pero esto ya esta solucionado ;)

:: Todas las grandes cosas que se disfrutan son el producto de las pequeñas cosas que se logran ::