jueves, 2 de junio de 2011

A Game with Numbers

Nikifor and Trofim play the following game: they write some integer smaller then 2000000000 and take turns one after another. Nikifor is the first to make a move. The turn is made by the following rule: from the written integer any non-zero digit is subtracted, and the new integer replaces the old one on the desk. For example for integer 40534, the next move can be: 40530, 40531 or 40529. The winner is the player who writes zero on the desk.

Write a program to decide who will win if both players do their best.
Input

The input contains the integer from which the game is started.
Output

In the first line you must write 1 if Nikifor wins and 2 otherwise. If Nikifor wins then in the second line you must output the move in the first turn which guarantees victory for him. If there are many such moves then output any of them. 


Example
Input:
14

Output:
1
4


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



public class AGameWithNumbers 
{
   public static void main( String args[] ) throws Exception
   {  
      int ultimoDigito = System.in.read();
      int car = System.in.read();
  
      /*
      * Es facil de determinar de que el jugador 2 solo ganara
      * en caso de que el numero inicial termine en 0, en caso
      * contrario ganara el jugador 1, por tanto es suficiente
      * con saber cual es el ultimo digito del numero ingresado.
      */
      while( car != 10 && car != -1 )
      {
         ultimoDigito = car;
         car = System.in.read();
      }
         
      // Recordar que el 48 es el codigo ASCCI del 0
      if( ultimoDigito == 48 )
         System.out.println( 2 );
      else
      {
         System.out.println( 1 );
         System.out.println( ultimoDigito - 48 );
      }
   }
}

No hay comentarios:

Publicar un comentario