轉自 https://www.hackerrank.com/challenges/between-two-sets/problem
參考http://www.runoob.com/python/python-for-loop.html
循環(huán)使用 else 語句
在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區(qū)別,else 中的語句會在循環(huán)正常執(zhí)行完(即 for 不是通過 break 跳出而中斷的)的情況下執(zhí)行,while … else 也是一樣俩滥。
解決代碼
#!/bin/python
import sys
def getTotalX(a, b):
# Complete this function
m = 0
x = max(a)
y = min(b)
for i0 in range(x,y+1):
for i1 in range(len(a)):
if i0 % a[i1] == 0:
continue
else:
break
else:
for i2 in range(len(b)):
if b[i2] % i0 == 0:
continue
else:
break
else:
m += 1
return m
if __name__ == "__main__":
n, m = raw_input().strip().split(' ')
n, m = [int(n), int(m)]
a = map(int, raw_input().strip().split(' '))
b = map(int, raw_input().strip().split(' '))
total = getTotalX(a, b)
print total