Python求解特殊回文数

发布时间:2024年01月21日

运行超时版本


class Stack:
    def __init__(self):
        self.stack = []

    def push(self, item):
        self.stack.append(item)

    def pop(self):
        return self.stack.pop()

    def peak(self):
        return self.stack[self.size() - 1]

    def isEmpty(self):
        return self.stack == []

    def size(self):
        return len(self.stack)


class PalindromeAchieve:
    def __init__(self, nNumbers):
        self.nNumbers = nNumbers
        self.stack = Stack()

    def findpalindrome(self):
        for i in range(10001, 1000000):
            if self.judgepalindrome(str(i)):
                print(i)

    def judgepalindrome(self, number):
        index = 0
        n = len(number)
        outcome = ''
        sum = 0
        while index < n:
            self.stack.push(number[index])
            index += 1

        while not self.stack.isEmpty():
            pop = self.stack.pop()
            outcome = outcome + pop
            sum += int(pop)

        if outcome == number and sum == self.nNumbers:
            return True
        else:
            return False


n = int(input())
PalindromeAchieve(n).findpalindrome()


文章来源:https://blog.csdn.net/weixin_45977617/article/details/135715777
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。