Advertisement
Guest User

PALIN

a guest
Oct 9th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.math.BigInteger;
  4.  
  5. class PALIN_SPOJ {
  6.  
  7.     static final BigInteger ZERO = BigInteger.ZERO, ONE = BigInteger.ONE, TEN = BigInteger.TEN;
  8.    
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         BigInteger num = ZERO, copy = ZERO, fh = ZERO, sh = ZERO, newnum = ZERO;
  12.         try{num = new BigInteger(br.readLine());}catch(Exception e){};
  13.         copy = num;
  14.        
  15.         int c = 0;
  16.        
  17.         while(copy.compareTo(ZERO) == 1){
  18.             copy = copy.divide(TEN);
  19.             c++;
  20.         }
  21.        
  22.         copy = num;
  23.        
  24.         fh = copy.divide(pow(c/2));
  25.         sh = reverse(c%2 == 0? fh: fh.divide(TEN));
  26.         newnum = newnum.add(fh.multiply(pow(c/2)));
  27.         newnum = newnum.add(sh);
  28.        
  29.         if(newnum.compareTo(num) == -1 || newnum.compareTo(num) == 0 ){
  30.             newnum = ZERO;
  31.             fh = fh.add(ONE);
  32.             sh = reverse(c%2 == 0? fh: fh.divide(TEN));
  33.             newnum = newnum.add(fh.multiply(pow(c/2)));
  34.             newnum = newnum.add(sh);
  35.         }
  36.         System.out.println(newnum);
  37.     }
  38.  
  39.     private static BigInteger pow(int n) {
  40.         BigInteger ret = ONE;
  41.        
  42.         for(int i = 1; i <= n; i++)
  43.             ret = ret.multiply(TEN);
  44.        
  45.         return ret;
  46.     }
  47.  
  48.     private static BigInteger reverse(BigInteger n) {
  49.         BigInteger ret = ZERO;
  50.        
  51.         while(n.compareTo(ZERO) == 1){
  52.             ret = ret.multiply(TEN);
  53.             ret = ret.add(n.mod(TEN));
  54.             n = n.divide(TEN);
  55.         }
  56.        
  57.         return ret;
  58.    
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement