百度:笔试题(20190910)

本文探讨了两种算法问题的解决方案:一是翻硬币问题,通过特定策略计算最少翻转次数;二是取数问题,采用暴力求解法在限定回合内获取最大数值总和。代码示例使用Python实现。

翻硬币

没有AC的思路,就简单的考虑一种情况来实现的,通过率为45%。

def flip_corn(seq, x, y):
    if not seq:
        return 0
    neg_to_pos = min(x, y)
    count, flag = 0, False
    for su in seq:
        if flag:
            if su == 1:
                continue
            else:
                flag = False
                count += 1
        else:
            if su == 0:
                continue
            else:
                flag = True
    if not flag and seq[-1] == 0:
        count += 1
    return count * neg_to_pos


if __name__ == '__main__':
    n, x, y = list(map(int, input().strip().split()))
    seq = list(map(int, input().strip().split()))
    res = flip_corn(seq, x, y)
    print(res)

'''
3 1 2
0 1 0
'''

取数

暴力求解,针对数字个数n和回合数m生成一个全排列,然后对每一种情况依次按序求解,返回最大值。

from itertools import permutations


def get_number(value, cost):
    if not value:
        return 0
    vu, cu = value[0], cost[0]
    return vu + get_number([val-cu for val in value[1:]], cost[1:])


def shuffle(values, costs, n, m):
    index = list(permutations(range(0, n), m))
    res = 0
    for idx in index:
        value = [values[i] for i in idx]
        cost = [costs[i] for i in idx]
        cur = get_number(value, cost)
        # print(value, cost, cur)
        if cur > res:
            res = cur
    return res


if __name__ == '__main__':
    n = int(input().strip())
    m = int(input().strip())
    values = list(map(int, input().strip().split()))
    costs = list(map(int, input().strip().split()))
    res = shuffle(values, costs, n, m)
    print(res)

'''
5
4
10 20 30 40 50
4 5 6 7 8
'''

(最近更新:2019年09月12日)

百度2012 至今笔试题 以及简介 5.1 百度运维部笔试题................................................................................................................................17 5.2 百度商务搜索笔试题...................................................................................................................................18 5.3 百度商务搜索部笔试题...............................................................................................................................18 5.4 百度质量部笔试题.......................................................................................................................................18 5.5 百度质量部第一场笔试笔试题................................................................................................................19 5.6 百度RD-2卷(质量部等)笔试真题..........................................................................................................19 5.7 百度商业应用产品部(非技术类)笔试题................................................................................................21 5.8 百度商业产品部笔试归来~~~真题奉献......................................................................................................22 5.9 百度用户体验部笔试题...............................................................................................................................23 5.10 百度电子商务事业部笔试真题....................................................................................................................24 5.11 百度运维web开发两道笔试题...................................................................................................................24 5.12 百度技术类笔试真题...................................................................................................................................25 5.13 百度技术类笔试真题(原题扫描)............................................................................................................25 5.14 百度技术研发笔试题目...............................................................................................................................29 5.15 百度笔试题...................................................................................................................................................36 5.16 百度笔试题目--回忆版 ................................................................................................................................36 5.17 百度笔试题(市场部) ....................................................................................................................................37 5.18 用户体验笔试题...........................................................................................................................................40 5.19 2008-9-24百度笔试题(第一套题) ..........................................................................................................40 5.20 2008-9-24百度笔试题(第三套题) ..........................................................................................................42
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值