2018-02-02 for...else...解决一个HackerRank问题 Between Two Sets

转自 https://www.hackerrank.com/challenges/between-two-sets/problem
参考http://www.runoob.com/python/python-for-loop.html

循环使用 else 语句
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,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

你可能感兴趣的:(2018-02-02 for...else...解决一个HackerRank问题 Between Two Sets)