Problem:


回文数

Problem Statement

整数nに最も近い回文数を求めよ．

なお，非負整数xが回文数であるとは，xを十進法で表現した文字列とそれを反転させた文字列が等しいことをいう．

例えば0,7,33,10301などは回文数であり，32,90,1010などは回文数でない．

Input

入力は以下の形式に従う．与えられる数は全て整数である．

n

Constraints

1≦n≦10^4

Output

nに最も近い回文数を出力せよ．

そのような数が複数ある場合は最も小さいものを出力せよ．

Sample Input 1

13

Output for the Sample Input 1

11

13に最も近い回文数は11である．

Sample Input 2

7447

Output for the Sample Input 2

7447

7447は回文数なのでそのまま出力すればよい．

Sample Input 3

106

Output for the Sample Input 3

101

106に最も近い回文数は111と101のふたつあるが，そのうち小さい方の101を出力する．



Sample input:
13


Sample output:
11

Submission status: Wrong Answer

Solution code:
def check(n):
    n = str(n)
    if n == n[::-1]:
        return True
    else:
        return False


n = int(input())
if check(n):
    print(n)

else:
    base = n
    while 1:
        n -= 1
        if check(n):
            ans = n
            break

    diff = base - n
    n = base
    for i in range(n, base):
        if check(i):
            print(i)
            break
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
def palind(n):
  return str(n) == str(n)[::-1]

n = input()
for i in range(10):
  if n >= i and palind(n - i):
    print n - i
    break
  if palind(n + i):
    print n + i
    break

Submission status: Wrong Answer

Solution code:
n=input()
for i in range(10000-n):
    a,b=str(n+i),str(n-i)
    if b==b[::-1]:
        print b
        break
    if a==a[::-1]:
        print a
        break

Submission status: Wrong Answer

Solution code:
n=input()
for i in range(10000-n+1):
    a,b=str(n+i),str(n-i)
    if b==b[::-1]:
        print b
        break
    if a==a[::-1]:
        print a
        break

Submission status: Accepted

Solution code:
def check(n):
    n = str(n)
    if n == n[::-1]:
        return True
    else:
        return False


n = int(input())
if check(n):
    print(n)

else:
    base = n
    while 1:
        n -= 1
        if check(n):
            ans = n
            break

    diff = base - n
    n = base
    for i in range(base, base + diff):
        if check(i):
            print(i)
            break
    else:
        print(ans)

Submission status: Accepted

Solution code:
l1 = [i for i in range(10)]
l2 = [int(str(c) + str(c)) for c in range(1, 10)]
l3 = [int(str(c1) + str(c2) + str(c1)) for c1 in range(1, 10) for c2 in range(10)]
l4 = [int(str(c1) + str(c2) + str(c2) + str(c1)) for c1 in range(1, 10) for c2 in range(10)]
l5 = [10001]
lst = l1 + l2 + l3 + l4 + l5
n = int(input())
dist = 10 ** 6
ans = -1
for num in lst:
  if abs(num - n) < dist:
    dist = abs(num - n)
    ans = num
  elif abs(num - n) == dist and num < ans:
    ans = num
print(ans)

Submission status: Accepted

Solution code:
def f(n):
    n=str(n)
    if n==n[::-1]:return 1
a=b=int(input())
while 1:
    if f(b): print(b);break
    a-=1
    if f(a):print(a);break
    b+=1

Submission status: Accepted

Solution code:
n = int(input())

s = list(range(10))
for i in range(1, 100):
    t = str(i)
    s.append(t+t[::-1])
    for j in range(10):
        s.append(t+str(j)+t[::-1])

s = list(map(int, s))
s.sort()
if n in s:
    print(n)
else:
    if n < s[0]:
        print(s[0])
    elif n > s[-1]:
        print(s[-1])
    else:
        res = 0
        diff = 1e9
        for d in s:
            if diff > abs(d-n):
                diff = abs(d-n)
                res = d

        print(res)

Submission status: Accepted

Solution code:
def palind(n):
  return str(n) == str(n)[::-1]

n = input()
for i in range(100000):
  if n >= i and palind(n - i):
    print n - i
    break
  if palind(n + i):
    print n + i
    break

Submission status: Accepted

Solution code:
def palindromic(n):
	n = str(n)
	return all(x == y for x, y in zip(n[:len(n) / 2], n[::-1]))
def solve(n):
	for i in xrange(10001 - n + 1):
		if n - i >= 0 and palindromic(n - i):
			return n - i
		elif palindromic(n + i):
			return n + i
print solve(input())

Submission status: Accepted

Solution code:
n=input()
for i in range(30000):
    a,b=str(n+i),str(n-i)
    if b==b[::-1]:
        print b
        break
    if a==a[::-1]:
        print a
        break

Submission status: Accepted

Solution code:
n = input()
mind = n
for i in range(max(0,n-100),min(10001,n+100)):
	if i == int(str(i)[::-1]) and abs(n - i) < mind:
		mind = abs(n - i)
		ans = i
print ans

Submission status: Accepted

Solution code:
n1 = n2 = input()
while 1:
	if n1 > -1 and n1 == int(str(n1)[::-1]):
		print n1
		break
	elif n2 == int(str(n2)[::-1]):
		print n2
		break
	n1 -= 1
	n2 += 1

Submission status: Accepted

Solution code:
n = input()
i = 0
while 1:
	if n>=i and n-i == int(str(n-i)[::-1]):
		print n-i
		break
	elif n+i == int(str(n+i)[::-1]):
		print n+i
		break
	i += 1

Submission status: Accepted

Solution code:
n = input()
i = 0
while 1:
	n1,n2 = str(n-i), str(n+i)
	if n1 == n1[::-1]:
		print n1
		break
	elif n2 == n2[::-1]:
		print n2
		break
	i += 1

Submission status: Accepted

Solution code:
def isPalindrome(n):
    s = str(n); l = len(s);
    for i in xrange(l/2):
        if s[i]!=s[-1-i]:
            return False
    return True
n = input()
p = q = n
while isPalindrome(p)==False:
    p += 1
while isPalindrome(q)==False:
    q -= 1
print q if abs(n-q)<=abs(p-n) else p

Submission status: Accepted

Solution code:
def isPalindrome(n):
    s = str(n)
    return s == s[::-1]
n = input()
p = q = n
while isPalindrome(p)==False:
    p += 1
while isPalindrome(q)==False:
    q -= 1
print q if abs(n-q)<=abs(p-n) else p

Submission status: Accepted

Solution code:
import bisect
rev=[]
for i in range(1,10002):
    if str(i)==str(i)[::-1]:rev.append(i)
n=int(input())
ind= bisect.bisect_left(rev,n)
if abs(n-rev[ind-1])<=abs(n-rev[ind]):
    print(rev[ind-1])
else:
    print(rev[ind])

Submission status: Runtime Error

Solution code:
n = input()
mind = n
for i in range(max(0,n-20),min(10001,n+20)):
	if i == int(str(i)[::-1]) and abs(n - i) < mind:
		mind = abs(n - i)
		ans = i
print ans