S先生与P先生谜题,谜题,[Python]代码de


[Python]代码

def gatherBy(seq, f):    d = {}    for x in seq:        d.setdefault(f(x), []).append(x)    return dpool = [(a, b) for a in range(2, 98 + 1) for b in range(2, a + 1)]Sums = gatherBy(pool, lambda x: x[0] + x[1])Prods = gatherBy(pool, lambda x: x[0] * x[1])def MrP_dont_know(p):    return len(Prods[p]) != 1def MrS_dont_know(s):    return len(Sums[s]) != 1def MrS_know_MrP_doesnt_know(s):    return all([MrP_dont_know(a * b) for a, b in Sums[s]])def MrP_now_knows(p):    return len([(a, b) for a, b in Prods[p] if MrS_know_MrP_doesnt_know(a + b)]) == 1def MrS_knows_MrP_now_know(s):    return len([(a, b) for a, b in Sums[s] if MrP_now_knows(a * b)]) == 1for a, b in pool:    s = a + b    p = a * b    if MrP_dont_know(p) and MrS_dont_know(s) and MrS_know_MrP_doesnt_know(s)\       and MrP_now_knows(p) and MrS_knows_MrP_now_know(s):        print a, b#result : 13 4

评论关闭