首页 > 网易笔试 8.8 算法岗 ac3道 Python
头像
lih627
编辑于 2020-08-10 19:29
+ 关注

网易笔试 8.8 算法岗 ac3道 Python

AC了三道,第四道不会…

第一题

直接统计单词数量就可以了,都转化小写

import collections
if __name__=='__main__':
    n = int(input())
    counter = collections.defaultdict(int)
    for _ in range(n):
        word = input()
        word = word.lower()
        counter[word] += 1
    ret = 0

    for k, v in counter.items():
        if v * 100 >= n:
            ret += 1
    print(ret)

第二题

大意是有n个物品,每个都有各自的价值,然后删去一些,分两个人,让每个人得到物品的价值相等。n最大为15
那么所有删除的情况为 种,直接暴力就好了,枚举所有删除情况,然后看一下剩下的能否均分就可以

def isValid(nums):
    sum_ = sum(nums)
    if sum_ & 1:
        return False
    mid = sum_ // 2
    visited = set()
    visited.add(0)
    for num in nums:
        idxs = list(visited)
        for idx in idxs:
            visited.add(idx + num)
    return mid in visited


def solve(n, nums):
    ret = float('inf')
    # 枚举所有删除情况
    for mask in range(2 ** n):
        select = []
        unselect = 0
        for i in range(n):
            if mask & (1 << i):
                unselect += nums[i]
            else:
                select.append(nums[i])
        # print(select, unselect)
        if ret <= unselect:
            continue
        else:
            if isValid(select):
                ret = unselect
    print(ret)


if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        nums = list(map(int, input().split()))
        solve(n, nums)

第三题

比较基础的DP吧,主要坑在比如了12:30:23这种情况。竟然输出结果是am
我是倒着dp的,代码有点乱将就着看吧…

def solve(n, a, b):
    if n == 1:
        ret = a[0]
    else:
        buy1 = [a[-1], 0]
        buy2 = [float('inf'), 0]
        for j in range(n - 2, -1, -1):
            cur1 = a[j] + min(buy1[0], buy2[0])
            cur2 = b[j] + min(buy1[1], buy2[1])
            buy1[1] = buy1[0]
            buy1[0] = cur1
            buy2[1] = buy2[0]
            buy2[0] = cur2
        ret = min(buy1[0], buy2[0])
    isam = True
    hour = ret // 3600
    tt = 8 + hour
    while tt >= 13:
        tt -= 12
        isam = not isam
    ret -= hour * 3600
    minute = ret // 60
    ret -= minute * 60
    second = ret
    suffix = 'am' if isam else 'pm'
    print("{:02d}:{:02d}:{:02d} {}".format(tt, minute, second, suffix))


if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        a = list(map(int, input().split()))
        b = list(map(int, input().split()))
        solve(n, a, b)

Ps: 求各位大佬帮忙内推…快找不到工作了

全部评论

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

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐