1. 程式人生 > 其它 >CCF-CSP第三題彙總(python)

CCF-CSP第三題彙總(python)

技術標籤:CSP&CCSPpython

前言

暫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")

在這裡插入圖片描述