viernes, 3 de junio de 2011

Relax! It is just a game

 You: What's the score? Did I miss much?

Me: It's 2-1 for elAhli and the second half just started. The first half was quite boring.

You: Who scored first? elAhli or ezZamalek?

Me: What difference does it make?

You: Big difference! I can predict the outcome of the match if I knew the order of which goals were scored in the first half.

Me: What do you mean?

You: It's 2-1 for elAhli, right? One of three things could have happened: elAhli scored two goals then ezZamalek scored; Or, elAhli scored its first goal, then ezZamalek, then elAhli again; Or, ezZamalek scored first, then elAhli scored its two goals.

Me: So?!! I still don't understand what difference does that make? It's still 2-1 for elAhli! Why don't you just relax and let us continue watching the game in peace.

You: You don't understand!! I believe the probability of who'll win depends on the order of how goals were scored. Now I have to predict the outcome for 3 possibilities.

Me: And what if the score was 3-2? What would you have done then?

You: I would have to work for 5 different possibilities. No?

Me: Of course not! The number of possibilities isn't always equal to the sum.

You: Can you tell me when will it be equal to the sum?

Me: You're a programmer, why don't you write a program that counts the number of possibilities and compare it to the sum?

You: I don't have the time, I want to watch the match. Besides, I have nine other problems to worry about.

Me: I'll give you a hint. The possibilities will be equal to the sum only if one of the teams scored a certain number of goals.
Input

Your program will be tested on one or more test cases. Each test case specifies two natural numbers (A and B ) (separated by one or more spaces) representing the score of the first half. No team will be able to score more than 10 goals. The last line of the input file contains two -1's (which is not part of the test cases.)
Output

Format For each test case where the number of possibilities is equal to the sum, print:

A+B=C

Where A and B are as above and C is their sum. If the number of possibilities is not equal to the sum, replace the '=' sign with '!=' (without the quotes.) 


Example
Input:
2 1
1 0
-1 -1

Output:
2+1=3
1+0=1


https://www.spoj.pl/problems/ANARC08E/




public class RelaxItisjustagame 
{
   public static void main( String args[] ) throws Exception
   {
      int car;
      int golesEquipoA, golesEquipoB, golesTotales;
  
      // Buffer de salida que mostrara la salida del programa
      char[] salida = new char[90000 ];
      int tamanio = 0;
      String cadena;
  
      while( true )
      {
         car = System.in.read();
   
         if( car == '-' )
            break;
   
         // Calculamos los goles que anoto el primer equipo
         golesEquipoA = car - 48;  
         car = System.in.read();
      
         if( car != 32 )
         {
            golesEquipoA = 10;
            car = System.in.read();
         }
   
         // Nos saltamos los espacios en blanco.
         do
            car = System.in.read();
         while( car == 32 );
   
         // Calculamos los goles que anoto el segundo equipo
         golesEquipoB = car - 48;
         car = System.in.read();
   
         if( car != 13 )
         {
            golesEquipoB = 10;
            car = System.in.read();
         }
   
         // Nos saltamos el caracter de fin de linea '\n'
         System.in.read();
   
         golesTotales = golesEquipoA + golesEquipoB;
   
         /*
         * Sean a y b los goles de cada equipo, entonces la probabilidad es:
         * ( a + b )!/a!b!, si lo igualamos a la suma de a y b:
         * 
         * ( a + b )!/a!b! = ( a + b ) -> ( a + b - 1 )!/a!b! = 1
         * 
         *  La unica solucion de la ecuacion anterior es cuando a = 1 ó b = 1
         */
         if( golesEquipoA == 1 || golesEquipoB == 1 )
            cadena = golesEquipoA + "+" + golesEquipoB + "=" + golesTotales + "\n";
         else
            cadena = golesEquipoA + "+" + golesEquipoB + "!=" + golesTotales + "\n";
    
         // Anadimos la respuesta al buffer de salida
         cadena.getChars( 0, cadena.length(), salida, tamanio );
         tamanio = tamanio + cadena.length();
      }
  
      // Mostramos el buffer
      System.out.print( new String( salida, 0, tamanio ) );
   }
}

No hay comentarios:

Publicar un comentario