竞赛讨论区 > A和C题,小白解法,不知道有哪些问题,望大佬赐教
头像
kekelele
发布于 2022-04-28 22:35
+ 关注

A和C题,小白解法,不知道有哪些问题,望大佬赐教

A题感觉没啥难得,但是为啥提交就全部没通过,就迷惑

while True:
    try:
        n = int(input())
        nums = list(map(int, input().split()))
        targetnums = list(map(int, input().split()))
    except:
        break

    # 计算由nums变换成targetnums所需的步数,每一步都将一个数字左移若干位
    step = 0
    j = 0
    for i in range(len(targetnums)):
        if targetnums[i] == nums[j]:
            j+=1
            continue
        else:
            step += 1
    print(step)

C题本来还想什么动态规划,后干脆回溯吧,感觉也没啥问题,但又是啥也没通过,起码能过个三分之一吧,为啥为啥,大佬求赐教!

import collections

while True:
    try:
        n = int(input())
        target = list(map(int, input().split()))
    except:
        break
    plan = []
    for i in range(n):
        plan.append(list(map(int, input().split())))


    plan = [[-2, 0], [3, 0], [4, 0], [5, 0], [0, 10], [0, -10], [0, 10]]
    target = [5, 10]

def com(candidates, target):

    path = []
    res = []
    candidates.sort()
    print(candidates)

    def backtracking(andidates, target, sum_, start_index):
        if sum_ == target:
            res.append(path[:])
            return

        # 单层递归逻辑
        for i in range(start_index, len(candidates)):

            sum_[0] += candidates[i][0]
            sum_[1] += candidates[i][1]
            path.append(candidates[i])
            backtracking(candidates, target, sum_, i+1)
            path.pop()             
            sum_[0] -= candidates[i][0]
            sum_[1] -= candidates[i][1]      

    backtracking(candidates, target, [0,0], 0)
    return res

res = com(plan, target)
# print(res)
cnt = []
for i in range(len(res)):
    cnt.append(len(res[i]))
# print(cnt)
ans = collections.Counter(cnt)
# print(ans)
for i in range(1, n+1):
    if i in ans:
        print(ans[i])
    else:
        print(0)

全部评论

(1) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐