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

CCF-CSP第二題彙總(python)

技術標籤:CSP&CCSPpython

文章目錄

前言

暫6

202009-2風險人群篩查

n, k, t, x_l, y_d, x_r , y_u = list(map(int, input().split()))

pass_by = 0
stay = 0

def judge(x,y,x_l, y_d, x_r, y_u):
    return x_l <= x <= x_r and y_d <= y <= y_u

for i in
range(n): dot_lst = list(map(int, input().split())) tmp_pass = 0 tmp_stay_cot = 0 tmp_stay = 0 for j in range(t): x = dot_lst[2*j] y = dot_lst[2*j+1] is_in = judge(x,y,x_l, y_d, x_r, y_u) if is_in: tmp_pass = 1 tmp_stay_cot +=
1 if tmp_stay_cot>=k: tmp_stay = 1 break else: tmp_stay_cot = 0 pass_by += tmp_pass stay += tmp_stay print(pass_by) print(stay, end="")

在這裡插入圖片描述

202006-2稀疏向量

n, a, b = list(map(int, input().split()))

a_dict = {}
for i in range
(a): index,value = list(map(int, input().split())) a_dict[index] = value ans = 0 for j in range(b): index,value = list(map(int, input().split())) if index in a_dict: ans += value * a_dict[index] del a_dict[index] print(ans)

在這裡插入圖片描述

201912-2回收站選址

"""
測試點9、10: (2*10**9)**2/8/1024/1024 = ...
"""
ans_lst = [0,0,0,0,0]
dot_dict = {}
judge1_lst = [(-1,0),(0,1),(1,0),(0,-1)]
judge2_lst = [(-1,1),(1,1),(1,-1),(-1,-1)]
n = int(input())


def check(dot_dict, x,y):
    # left up right down
    for i in judge1_lst:
        new_x,new_y = x+i[0], y+i[1]
        if (new_x,new_y) in dot_dict:
            dot_dict[(new_x,new_y)][0] += 1
            dot_dict[(x, y)][0] += 1
    for i in judge2_lst:
        new_x, new_y = x + i[0], y + i[1]
        if (new_x, new_y) in dot_dict:
            dot_dict[(new_x, new_y)][1] += 1
            dot_dict[(x, y)][1] += 1
    return dot_dict

for i in range(n):
    x,y = list(map(int, input().split()))
    dot_dict[(x,y)] = [0,0]
    dot_dict = check(dot_dict, x,y)

for v in dot_dict.values():
    if v[0] == 4:
        ans_lst[v[1]] += 1

print(ans_lst[0])
print(ans_lst[1])
print(ans_lst[2])
print(ans_lst[3])
print(ans_lst[4], end="")

在這裡插入圖片描述

201909-2小明種蘋果(續)

T = D = E = 0
N = int(input())
have_drop = []
for i in range(N):
    tmp_D = 0
    input_lst = list(map(int, input().split()))
    apple_num = input_lst[1]
    for j in input_lst[2:]:
        if j <= 0:
            apple_num += j
        else:
            if j != apple_num:
                tmp_D = 1
                apple_num = j
    T += apple_num
    have_drop.append(tmp_D)

for idx, i in enumerate(have_drop):
    if i == 1:
        D += 1
        if idx == 0:
            pre = N-1
            suc = idx+1
        elif idx == N-1:
            pre = idx - 1
            suc = 0
        else:
            pre = idx - 1
            suc = idx + 1
        if have_drop[pre] * have_drop[suc] == 1:
            E += 1
print(T,end=" ")
print(D,end=" ")
print(E,end="")

在這裡插入圖片描述

201903-2二十四點

"""
加減法:兩個棧
1.數字:直接入棧
2.符號:
    如果符號棧的第一個符號的優先順序比當前符號低,則入棧
    否則,取出棧中所有優先順序較高的,做運算
最後把棧中剩下的,都計算完,
"""
def operate_(num1,num2,op):
    if op == "+":
        ans = num1 + num2
    elif op == "-":
        ans = num1 - num2
    elif op == "x":
        ans = num1 * num2
    else:
        ans = num1 // num2
    return ans

n = int(input())
prior_dict = {"+":1,"-":1,"x":2,"/":2}
for i in range(n):
    num_stack = []
    op_stack = []
    stringg = input()
    for j in stringg:
        if j.isdigit():
            num_stack.append(int(j))
        else:  # 符號
            if len(op_stack) == 0 or prior_dict[op_stack[-1]] < prior_dict[j]:
                op_stack.append(j)
            else:
                while len(op_stack) != 0 and prior_dict[op_stack[-1]] >= prior_dict[j]:
                    p_j = op_stack.pop()
                    num2 = num_stack.pop()
                    num1 = num_stack.pop()
                    tmp_ans = operate_(num1,num2,p_j)
                    num_stack.append(tmp_ans)
                op_stack.append(j)
    while len(op_stack) != 0:
        op = op_stack.pop()
        num2 = num_stack.pop()
        num1 = num_stack.pop()
        tmp_ans = operate_(num1,num2,op)
        num_stack.append(tmp_ans)
    if num_stack[0] == 24:
        print("Yes")
    else:
        print("No")

在這裡插入圖片描述