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
37
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
45
46
47
48
49
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
71
72
73
74
75 public static void main( String[] args ) throws Exception
76 {
77 new odometer().doit();
78
79
80
81
82
83
84
85
86
87 }
88
89 }