问题描述
1221是一个非常特殊的数,它从左边读和从右边读是一样的,编程求所有这样的四位十进制数。
输出格式
按从小到大的顺序输出满足条件的四位十进制数。
下面给出两种解答方案
- 第一种采用列表存储。
for x in range(1000,10000):
z=str(x)
y=[]
for i in z:
y.append(i)
first=y.pop(0)
sencond=y.pop(0)
third=y.pop(0)
fourth=y.pop(0)
if first==fourth and sencond==third:
print(x)
- 第二种采用字符串获取子串的方法实现。该方案代码长度小于方案一,且提交系统显示的CPU运行时间明显少于方案一。
for x in range(1000,10000):
z=str(x)
first=z[0:1]
second=z[1:2]
third=z[2:3]
fourth=z[3:4]
if first==fourth and second==third:
print(x)