코딩

[BOJ] 1334 다음 팰린드롬 수

척척석사아님 2023. 12. 31. 17:30
728x90
def f(n):
    length = len(n)
    if len(n) == 1:
        if n == "9":
            return 11
        else:
            return int(n) + 1
    k = length // 2
    if length % 2 == 0:
        num = int(n[:k] + n[::-1][k:])
        if num > int(n):
            return int(num)
        else:
            number = str(int(n[:k]) + 1)
            if len(number) > k:
                num = number[:-1] + number[::-1]
            else:
                num = number + number[::-1]
            return int(num)
    else:
        num = int(n[:k+1] + n[::-1][k+1:])
        if num > int(n):
            return int(num)
        else:
            number = str(int(n[:k+1]) + 1)
            if len(number) > k + 1:
                number = str(int(number) // 10)
                num = number + number[::-1]
                return int(num)
            else:
                num = number[:-1]+ number[::-1]
                return int(num)
N = input()
print(f(N))

 

어렵지는 않았던 것 같다 !