Next Palindrome

Problem #323

Tags: simple c-1 strings

Who solved this?

No translations... yet

This problem was created by Clive Fraser aka CSFPython - many thanks!

Numbers which are palindromes read the same forwards and backwards. So 35453, 2992 and 6 are palindromes but 1121 is not. In this problem you are given a number and must find the smallest positive amount that can be added to the number in order to create a palindrome. For example, for the number 5862 we need to add 23 to obtain the palindrome 5885. 23 is the smallest addition for which we can create a palindrome. So 5885 is the next palindrome after 5862. If the starting number is already a palindrome you must find the smallest positive number which, when added to it, creates another palindrome. So, for an input of 373 we must add 10 to obtain the next palindrome 383. None of the input numbers will contain more than 18 digits.

Input and Answer
The first line of the problem will be a single integer N denoting the number of test cases.
Each of the following N lines will hold a single number. For each number you must find the smallest addition to it (greater than 0) which will create a palindrome. Give these answers, separated by spaces, as a single string.

Example:

input:
9
1
46
329
7307
55733
203092
6682635
22069658
826641918

answer:
1 9 4 30 22 210 231 7364 4710
You need to login to get test data and submit solution.