The Java Program: F_palindrometer/odometer.java

  1 import java.io.*;
  2 import java.util.*;
  3 
  4 public class odometer 
  5 {
  6     public Scanner sc;
  7     public PrintStream ps;
  8     
  9     public long getValue( String s )
 10     {
 11         long value = 0;
 12         for( int i=0; i<s.length(); i++ )
 13         {
 14             value *= 10;
 15             value += (int)(s.charAt(i) - '0');
 16         }
 17         
 18         return value;
 19     }
 20     
 21     public long getTarget( long half, int len )
 22     {
 23         long result = half;
 24         if( len%2 == 1 ) half /= 10;
 25         for( int i=len/2; i>0; i-- )
 26         {
 27             result *= 10;
 28             result += half%10;
 29             half /= 10;
 30         }
 31         
 32         return result;
 33     }
 34     
 35     /**
 36      * Driver.
 37      * @throws Exception
 38      */
 39     public void doit() throws Exception
 40     {
 41         sc = new Scanner( new File( "palindrometer.judge" ) );
 42         ps = new PrintStream( new FileOutputStream( "palindrometer.out" ) ); 
 43         
 44 //        for( long i=0; i<100000; i++ )
 45 //        {
 46 //            long input = getTarget( i, 10 ) + 1;
 47 //            String s = "000000000"+input;
 48 //            System.out.println( s.substring( s.length()-10, s.length() ) );
 49 //            ps.println( s.substring( s.length()-10, s.length() ) );
 50 //        }
 51         
 52         for(;;)
 53         {
 54             String reading = sc.next();
 55             if( reading.length()==1 ) break;
 56             
 57             long value = getValue( reading );
 58             int len = reading.length();
 59             
 60             long half = getValue( reading.substring( 0, (len-1)/2+1 ) );
 61             long target = getTarget( half, len );
 62             if( target < value ) target = getTarget( half+1, len );
 63             
 64             ps.println( (target-value) );
 65             System.out.println( (target-value) );
 66         }
 67     }
 68     
 69     /**
 70      * The main just creates an object and calls doit().
 71      * 
 72      * @param args Command line args, necessary but unused.
 73      * @throws Exception
 74      */
 75     public static void main( String[] args ) throws Exception
 76     {
 77         new odometer().doit();
 78 //        Random random = new Random();
 79 //        for( int i=0; i<10; i++ )
 80 //        {
 81 //            for( int j=0; j<9; j++ )
 82 //            {
 83 //                System.out.print( random.nextInt( 10 ) ); 
 84 //            } 
 85 //            System.out.println();
 86 //        }
 87     }
 88 
 89 }