martes, 31 de mayo de 2011

Number Steps

Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,... as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3, 1) respectively and this pattern has continued.







You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0...10000.


Input


The first line of the input is N, the number of test cases for this problem. In each of the N following lines, there is x, and y representing the coordinates (x, y) of a point.


Output


For each point in the input, write the number written at that point or write No Number if there is none.


Example
Input:
3
4 2
6 6
3 4


Output:
6
12
No Number

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





public class NumberStep 
{
   public static void main( String args[] ) throws Exception
   {
      // Almacena la respuesta actual
      String cad;           
      // Mensaje Error
      String error = "No Number\n";     
  
      // Buffer caracteres que se mostrara en la salida
      char[] salida = new char[ 90000  ];
      
      // Tamanio de la salida ( cantidad de caracteres )
      int tamanio = 0;
  
      int longitud;
      int x, y;
      int valXY, car;
      int cantPuntos = 0;
  
      // Leemos la cantidad de puntos que se ingresaran
      car = System.in.read();
  
      while( car != 10 )
      {
         cantPuntos = cantPuntos * 10 + ( car - 48 );
         car = System.in.read();
      }
  
      for( int i = 0; i < cantPuntos; i ++  )
      {
         x = 0;
         y = 0;
   
         // Obtenemos el valor de x
         car = System.in.read();
   
         while( car != 32 )
         {
            x = x * 10 + ( car - 48 );
            car = System.in.read();
         }
   
         // Obtenemos el valor de y
         car = System.in.read();
   
         while( car != 10 )
         {
            y = y * 10 + ( car - 48 );
            car = System.in.read();
         }
   
         // Si las coordenadas son correctas, hallamos el valor 
         // correspondiente a esas coordenadas
         if( x == y || x - y == 2 )
         {
            valXY = x << 1;
    
            if( ( x & 1 ) != 0 )
               valXY = valXY - 1;
    
            if( x == y )
               cad = valXY + "\n";
    
            else
               cad = ( valXY - 2 ) + "\n";
    
            // Copiamos la respuesta actual al buffer
            longitud = cad.length();
            cad.getChars( 0, longitud, salida, tamanio );
            tamanio = tamanio + longitud;
         }
   
         // Si las coordenadas son incorrectas mostramos el mensaje de error
         else
         {
            longitud = 10; 
            error.getChars( 0, longitud, salida, tamanio );
            tamanio = tamanio + longitud;
         }
      }
  
      // Mostramos el buffer donde se encuentran las respuestas
      System.out.print( new String( salida, 0, tamanio ) );
   }
}

No hay comentarios:

Publicar un comentario