Python求解特殊回文数

运行超时版本


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()


你可能感兴趣的:(python,开发语言)