检查前一个数字是当前数字的约数的最长连续序列
Check for the longest continuous sequence where the previous number is a divisor of the current number
我当前读取整数直到输入 -1 并检查 even/odd/even 的最长模式的代码如何修改为检查最长连续序列,其中前一个数字是当前数字的除数.
代码:
longest=[]
current=[]
while True:
n = int(input())
if n == -1:
break
if current and current[-1] % 2 != n % 2:
current.append(n)
else:
current = [n]
if len(current) > len(longest):
longest = current
print(len(longest))
最近的尝试:
代码:
longest=[]
current=[]
while True:
n = int(input())
if n == -1:
break
if current % current[-1] == 0:
current.append(n)
else:
current = [n]
if len(current) > len(longest):
longest = current
print(len(longest))
因为current[-1]
是之前的数字,你可以简单的改成:
if current and current[-1] % 2 != n % 2:
至:
if current and n % current[-1] == 0:
示例输入:
1
2
4
8
2
4
-1
会输出:
4
我当前读取整数直到输入 -1 并检查 even/odd/even 的最长模式的代码如何修改为检查最长连续序列,其中前一个数字是当前数字的除数.
代码:
longest=[]
current=[]
while True:
n = int(input())
if n == -1:
break
if current and current[-1] % 2 != n % 2:
current.append(n)
else:
current = [n]
if len(current) > len(longest):
longest = current
print(len(longest))
最近的尝试: 代码:
longest=[]
current=[]
while True:
n = int(input())
if n == -1:
break
if current % current[-1] == 0:
current.append(n)
else:
current = [n]
if len(current) > len(longest):
longest = current
print(len(longest))
因为current[-1]
是之前的数字,你可以简单的改成:
if current and current[-1] % 2 != n % 2:
至:
if current and n % current[-1] == 0:
示例输入:
1
2
4
8
2
4
-1
会输出:
4