逻辑计算题

from itertools import product

def check_conditions(suspects):
    A, B, C, D, E, F = suspects
    
    # 1. A和B至少一人作案
    cond1 = (A + B >= 1)
    
    # 2. A,E,F中至少两人作案
    cond2 = (A + E + F >= 2)
    
    # 3. A和D不能同时作案
    cond3 = not (A and D)
    
    # 4. B和C同时作案或同时无关
    cond4 = (B == C)
    
    # 5. C和D仅一人作案
    cond5 = (C != D)
    
    # 6. 如果D未作案,则E也未作案
    cond6 = (D or not E)  # D为真 或 E为假
    
    return all([cond1, cond2, cond3, cond4, cond5, cond6])

# 生成所有可能的嫌疑人组合 (2^6=64种)
possible_combinations = product([0, 1], repeat=6)

# 检查所有组合并输出有效解
solutions = []
for comb in possible_combinations:
    if check_conditions(comb):
        solutions.append(comb)

# 输出结果
if solutions:
    print("满足所有条件的作案组合:")
    for sol in solutions:
        A, B, C, D, E, F = sol
        print(f"A={A}, B={B}, C={C}, D={D}, E={E}, F={F}")
else:
    print("无解")

  

from itertools import product

def check_conditions(suspects):
    A, B, C, D, E, F = suspects
    cond1 = (A + B >= 1)
    cond2 = (A + E + F >= 2)
    cond3 = not (A and D)  # A和D不能同时为1
    cond4 = (B == C)
    cond5 = (C != D)
    cond6 = (D or not E)   # D=1 或 E=0
    return all([cond1, cond2, cond3, cond4, cond5, cond6])

solutions = []
for comb in product([0,1], repeat=6):
    if check_conditions(comb):
        solutions.append(comb)

if solutions:
    print("有效作案组合:")
    for sol in solutions:
        print(f"A={sol[0]}, B={sol[1]}, C={sol[2]}, D={sol[3]}, E={sol[4]}, F={sol[5]}")
else:
    print("无解")

  刑侦大队对涉及6个嫌疑人的一桩疑案进行分析: 1、 A和B至少有一人作案; 2、 A、E、F三人中至少有2人参与作案; 3、 A、D不可能是同案犯; 4、B、C或同时作案,或与本案无关; 5、C、D有且仅有一人作案; 6、如果D没有参与作案,则E也不可能参与作案 请用符号逻辑学给出计算公式,并用python写出逻辑算法。

posted @ 2025-08-11 09:45  华腾智算  阅读(8)  评论(0)    收藏  举报
https://damo.alibaba.com/ https://tianchi.aliyun.com/course?spm=5176.21206777.J_3941670930.5.87dc17c9BZNvLL