这个问题只有唯一解。
程序代码:
def sum_split(Sum, low, high):
ans = []
for a in range(low, high+1):
b = Sum - a
if b <= a:
break
if b <= high:
ans.append((a,b))
return ans
def porduct_split(Product, low, high):
ans = []
for a in range(low, high+1):
b = Product // a
if b <= a:
break
if Product%a == 0 and b <= high:
ans.append((a,b))
return ans
def 由和可以直接得ab(A):
"""
和为A,是否能直接推出a,b
若能直接推出a,b返回True
若不能直接推出a,b则返回False
"""
sp = sum_split(A, 2, 99)
return len(sp) == 1
def 由积可以直接得ab(B):
"""
乘积为B,是否能直接推出a,b
若能直接推出a,b返回True
若不能直接推出a,b则返回False
"""
sp = sum_split(A, 2, 99)
return len(sp) == 1
def F(A):
"""
F(A)==1
验证A是否符合到庞统的第二句话,但是我肯定你(指孙膑)也不知道这两个数是什么
如果是True将返回所有可能的B;如果False,则第二个参数返回的是[]
"""
# if not 由和可以直接得ab(A):
# return False
sp = sum_split(A, 2, 99)
possible_B = []
for this_case in sp:
a, b = this_case[0], this_case[1]
pp = porduct_split(a*b, 2, 99)
if 1 == len(pp): # 分解方式唯一
return False, []
else:
possible_B.append(a*b)
return True, possible_B # 每种分拆情况都不唯一
def G(B):
"""
B(B)==1
验证B是否符合到孙膑的第二句话,但是听你这么一说,我现在能够确定这两个数字了
及返回所有F(A)为True(1)的A的列表
"""
# if not 由和可以直接得ab(A):
# return False
pp = porduct_split(B, 2, 99)
possible_A = []
for this_case in pp:
a, b = this_case[0], this_case[1]
A = a + b
FA, Bs = F(A)
if FA:
possible_A.append(A)
return len(possible_A) == 1, possible_A
def main():
possible_A1 = []
for i in range(2+3,98+99+1):
if not 由和可以直接得ab(i):
possible_A1.append(i)
print("possible_A1 =", possible_A1, "个数:", len(possible_A1))
possible_A2 = []
possbile_A2_dict = {}
for A in possible_A1:
is_possible, possible_B = F(A)
if is_possible:
possible_A2.append(A)
possbile_A2_dict[A] = possible_B
print("possible_A2 =", possible_A2, "个数:", len(possible_A2))
# 针对每种可能的A,给出每种情况下,B可能的取值。
print("针对每种可能的A,给出每种情况下,B可能的取值。")
for A, Bs in possbile_A2_dict.items():
print("A = {} 可能的B = {}".format(A, Bs))
# 所有可能的B的可能集的并集
B_candidates = set()
for Bs in possbile_A2_dict.values():
for B in Bs:
B_candidates.add(B)
print("B候选集大小", len(B_candidates))
possible_B4 = []
possible_B4_dict = {}
for B in B_candidates:
possible, possible_A = G(B)
if possible:
possible_B4.append(B)
possible_B4_dict[B] = possible_A[0]
possible_B4.sort()
print("Possible_B4 = ",possible_B4, "个数 =", len(possible_B4))
pk = 0
for B, A in possible_B4_dict.items():
pk += 1
print("(B = {: >3},A = {: >2})".format(B,A), end="\t")
if pk == 6:
print()
pk = 0
# 根据最后的庞统的话缩小范围
possible_B4_set = set(possible_B4)
possible_A5_set = set()
possible_AB_set = set()
for A, possible_B in possbile_A2_dict.items():
remain_B_set = set(possible_B) & possible_B4_set
print("A = {} 剩余的B = {}".format(A, remain_B_set))
if len(remain_B_set) == 1:
possible_A5_set.add(A)
B = list(remain_B_set)[0]
possible_AB_set.add((A, B))
print("目前为止(A,B)可能的解是:")
print(possible_AB_set)
if __name__ == "__main__":
main()
#F(11)