这个是 python2.7 的代码,如果是 python3.x 的话,改一下 print 函数就好。
if __name__ == '__main__':
list_three = []
list_six = []
list_nine = []
for i in range(1, 301):
if i % 3 == 0 or str(i).find('3') <> -1:
list_three.append(i)
if i % 6 == 0 or str(i).find('6') <> -1:
list_six.append(i)
if i % 9 == 0 or str(i).find('9') <> -1:
list_nine.append(i)
print '3 output: '
for item in list_three:
print item,
print ''
print '6 output: '
for item in list_six:
print item,
print ''
print '9 output: '
for item in list_nine:
print item,
print '
#说明一下:18是369的最小公倍数,取模。再匹配是不是全部数字为369。最后300以内只有36正确。
import re
RegEx = re.compile('^[369]+$')
for i in range(3001):
if (i % 18) == 0:
if RegEx.match(str(i)):
print('正确' + str(i))
else:
print('不包含' + str(i))
for i in range(1,301):
if i%3 == 0:
print(i)