def palind(n):
return [n*'%s'%tuple( list(i) + list(i[-(n%2)-1::-1]) ) for i in product(*( [range(1,10)]+[range(10)]*((n-1)/2)) )]
print map(palind, [3,4,5])
如果需要产生回运搏文数,一般会判断是否对称或比较反转后是旁森祥否还是自身然后过滤,范围较大时,效率就不高了
n位的回文数是其实是由前Ceiling(n/2)位决定的,以上代码采用构造春乎法生成回文数,生成10位以内的回文数<1s (不输出)
def ishuiweinum(num):if not isinstance(num,int):
return False
裤春迹 if num<0:
return False
numlist=[]
while num>胡并0:
numlist.append(num%10)
森团 num/=10
reverselist=numlist[:]
reverselist.reverse()
return reverselist==numlist
if __name__=='__main__':
print(ishuiweinum(12345))
print(ishuiweinum(1234321))
import turtle, mathdef pic(x0, y0, x1, y1):
dis = int(math.sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2))
turtle.hideturtle()
turtle.up()
turtle.goto(x0, y0)
turtle.down()
turtle.goto(x1, y1)
turtle.left(math.atan2(y1 橘虚- y0, x1 - 腊告x0) / math.pi * 180)
turtle.up()
turtle.backward(dis / 2)
turtle.write('长度为:%s' % dis, align='left')
turtle.done()
def palin():
k = []
total = []
for i in range(10, 200000):
while i >圆局燃 0:
k.append(i % 10)
i = i // 10
for m in range(len(k) // 2):
if k[m] != k[len(k) - m - 1]:
break
else:
print(k)
total.append(k)
k = []
print(len(total))
if __name__ == '__main__':
x0 = int(input('输入A的横坐标:'))
y0 = int(input('输入A的纵坐标:'))
x1 = int(input('输入B的横坐标:'))
y1 = int(input('输入B的纵坐标:'))
pic(x0, y0, x1, y1)
palin()
我把这两个放在一个主函数里运行了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)