33,027
社区成员




In [106]: def isLegal(n, i, j):
...: """取前i位为a1,第i+1到j位为a2,检测n是否由a1和a2生成的序列组成。"""
...: nextDigit = j
...: a1 = int(n[:i])
...: a2 = int(n[i:j])
...: while nextDigit<len(n): # n还有数字可用
...: a2, a1 = a1+a2, a2
...: s2 = str(a2)
...: if n.startswith(s2, nextDigit): #从n的第nextDigit位开始,与s2比较
...: nextDigit += len(s2)
...: else:
...: return False
...: return True
In [107]: def test(n):
...: for i in range(1, len(n)-1):
...: for j in range(i+1, len(n)-1):
...: if isLegal(n, i, j):
...: return True
...: return False
In [108]: test('1235814')
Out[108]: False
In [109]: test('1235813')
Out[109]: True
In [110]: test('112112224')
Out[110]: True
In [111]: test('1299111210')
Out[111]: True