Discuss / Python / 最开始是这样忽悠廖老师的,但是发现这个只能应付1000以内的数。

最开始是这样忽悠廖老师的,但是发现这个只能应付1000以内的数。

Topic source

FOREVER_574

#1 Created at ... [Delete] [Delete and Lock User]

def is_palindrome(n): str1 = str(n) if str1[0] == str1[-1]: return str1

FOREVER_574

#2 Created at ... [Delete] [Delete and Lock User]

但是呢,思考了一下,之前的只能应付1000以内的数,所有稍加修改了一下。 def is_palindrome(n):

#定义起始位置 int_start = 0

#定义结束位置 int_end = -1

#定义是否是回数,如果不是则+1 int_num = 1

#把传进来的int类型参数变为str类型 str1 = str(n)

#循环str for x in range(len(str1)):

#当首尾位置的数不相等的时候int_num加1并且跳出循环 if str1[int_end + x] != str1[int_start - x]: int_num += 1 break

#判断当int_num不等于2的时候说明是回数 if int_num != 2: return str1


  • 1

Reply