Advertisement
Guest User

SPOJ PALIN

a guest
Nov 7th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package spoj;
  2.  
  3. /*PALIN - The Next Palindrome
  4.  
  5. A positive integer is called a palindrome if its representation in the decimal system is the same when read from
  6. left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write
  7. the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
  8.  
  9. Input
  10. The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
  11.  
  12. Output
  13. For each K, output the smallest palindrome larger than K.*/
  14.  
  15. import java.util.Scanner;
  16.  
  17. public class PALIN {
  18.     public static void main(String[] args) throws java.lang.Exception {
  19.         Scanner scanner = new Scanner(System.in);
  20.         int t = scanner.nextInt();
  21.         for (int i = 0; i < t; i++) {
  22.             long k = scanner.nextLong();
  23.             k = k + 1;
  24.  
  25.             while (true) {
  26.                 char[] array = (String.valueOf(k)).toCharArray();
  27.                 int counter = 0;
  28.                 for (int n = 0; n <= array.length / 2; n++) {
  29.                     if (array[n] != array[array.length - n - 1]) {
  30.                         counter++;
  31.                         break;
  32.                     }
  33.                 }
  34.                 if (counter == 0) {
  35.                     System.out.println(k);
  36.                     break;
  37.                 }
  38.                 k++;
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement