CCF-CSP第三题汇总(python)

前言

暂1

202009-3点亮数字人生

前两个测试点(Q=1):最后print的有错误

def cal_one(func, num_lst):
    if func == "NOT":
        flag = not num_lst[0]
    elif func == "AND":
        flag = not 0 in num_lst
    elif func == "OR":
        flag = 1 in num_lst
    elif func == "XOR":
        flag = num_lst[0]
        for num in num_lst[1:]:
            flag = 1 if flag != num else 0
    elif func == "NAND":
        flag = 0 in num_lst
    else:
        flag = not 1 in num_lst
    if type(flag) == int:
        return flag
    return 1 if flag == True else 0

def cal_all(I_lst, cell_lst):
    tmp_O_lst = []
    for cell in cell_lst:
        func = cell[0]
        num_lst = [I_lst[int(i[1:])-1] if i[0] == "I" else tmp_O_lst[int(i[1:])-1] for i in cell[2:]]
        tmp_O_lst.append(cal_one(func, num_lst))
    return tmp_O_lst


Q = int(input())
for q in range(Q):
    M,N = list(map(int, input().split()))
    cell_lst = []
    O_lst = []
    for n in range(N):
        cell_lst.append(input().split())
    S = int(input())
    for s in range(S):
        I_lst = list(map(int, input().split()))
        O_lst.append(cal_all(I_lst, cell_lst))
    for s in range(S):
        input_lst = list(map(int,input().split()))
        cell_num = input_lst[0]
        for idx,cell_idx in enumerate(input_lst[1:]):
            if idx!= cell_num -1:
                print(O_lst[s][cell_idx-1], end=" ")
            else:
            	# 这里写错了
                if s != S-1:
                    print(O_lst[s][cell_idx-1])
                else:
                    print(O_lst[s][cell_idx-1], end="")

在这里插入图片描述
前5个测试点:没有考虑环

def cal_one(func, num_lst):
    if func == "NOT":
        flag = not num_lst[0]
    elif func == "AND":
        flag = not 0 in num_lst
    elif func == "OR":
        flag = 1 in num_lst
    elif func == "XOR":
        flag = num_lst[0]
        for num in num_lst[1:]:
            flag = 1 if flag != num else 0
    elif func == "NAND":
        flag = 0 in num_lst
    else:
        flag = not 1 in num_lst
    if type(flag) == int:
        return flag
    return 1 if flag == True else 0

def cal_all(I_lst, cell_lst):
    tmp_O_lst = []
    for cell in cell_lst:
        func = cell[0]
        num_lst = [I_lst[int(i[1:])-1] if i[0] == "I" else tmp_O_lst[int(i[1:])-1] for i in cell[2:]]
        tmp_O_lst.append(cal_one(func, num_lst))
    return tmp_O_lst


Q = int(input())
for q in range(Q):
    M,N = list(map(int, input().split()))
    cell_lst = []
    O_lst = []
    for n in range(N):
        cell_lst.append(input().split())
    S = int(input())
    for s in range(S):
        I_lst = list(map(int, input().split()))
        O_lst.append(cal_all(I_lst, cell_lst))
    for s in range(S):
        input_lst = list(map(int,input().split()))
        cell_num = input_lst[0]
        for idx,cell_idx in enumerate(input_lst[1:]):
            if idx!= cell_num -1:
                print(O_lst[s][cell_idx-1], end=" ")
            else:
                print(O_lst[s][cell_idx-1])

在这里插入图片描述
全部测试点

def cal_one(func, num_lst):
    if func == "NOT":
        flag = not num_lst[0]
    elif func == "AND":
        flag = not 0 in num_lst
    elif func == "OR":
        flag = 1 in num_lst
    elif func == "XOR":
        flag = num_lst[0]
        for num in num_lst[1:]:
            flag = 1 if flag != num else 0
    elif func == "NAND":
        flag = 0 in num_lst
    else:
        flag = not 1 in num_lst
    if type(flag) == int:
        return flag
    return 1 if flag == True else 0

def cal_all(I_lst, cell_lst):
    tmp_O_lst = []
    for cell in cell_lst:
        func = cell[0]
        num_lst = []
        for i in cell[2:]:
            if i[0] == "I":
                num_lst.append(I_lst[int(i[1:])-1])
            else:
                # circle
                if int(i[1:]) > len(tmp_O_lst):
                    return False
                else:
                    num_lst.append(tmp_O_lst[int(i[1:])-1])
                int(i[1:]) - 1
        # num_lst = [I_lst[int(i[1:])-1] if i[0] == "I" else tmp_O_lst[int(i[1:])-1] for i in cell[2:]]
        tmp_O_lst.append(cal_one(func, num_lst))
    return tmp_O_lst


Q = int(input())
for q in range(Q):
    M,N = list(map(int, input().split()))
    cell_lst = []
    O_lst = []
    for n in range(N):
        cell_lst.append(input().split())
    S = int(input())
    for s in range(S):
        I_lst = list(map(int, input().split()))
        result = cal_all(I_lst, cell_lst)
        O_lst.append(cal_all(I_lst, cell_lst))
    for s in range(S):
        input_lst = list(map(int,input().split()))
        if O_lst[s] != False:
            cell_num = input_lst[0]
            for idx,cell_idx in enumerate(input_lst[1:]):
                if idx!= cell_num -1:
                    print(O_lst[s][cell_idx-1], end=" ")
                else:
                    print(O_lst[s][cell_idx-1])
            else:
                continue
    # print LOOP
    if O_lst[0] == False:
        print("LOOP")

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值