【笔试代码题记录】190601

字节跳动

190531 字符串校正程序

题目链接 https://www.nowcoder.com/question/next?pid=16516564&qid=362291&tid=24204822
题目要求:在这里插入图片描述在这里插入图片描述

n = int(input())
while n > 0:
    ss = input()
    res = []
    for char in ss:
        if len(res) < 2:
            res.append(char)
            continue
        if len(res) >= 2:
            if char == res[-1] and char == res[-2]:
                continue
        if len(res) >= 3:
            if char == res[-1] and res[-2] == res[-3]:
                continue
        res.append(char)
    n -= 1
    print("".join(res))

190601 有限制条件的 C n 2 C^2_n Cn2问题

在这里插入图片描述在这里插入图片描述
思路分析:设置两个指针left和right,left作为外层循环遍历,每一次固定left,意味着将第一个特工固定在left位置,然后right指针右移,一直到无法移动为止(移到最后或者范围超界),计算left和right之间的空位假设为num个,则后两个特工的站位共有 C n u m 2 C^2_{num} Cnum2种,然后依次遍历left。注意边界条件。

n, disk = map(int, input().split())
nums = list(map(int, input().split()))

res = 0
left, right = 0, 2
while left < n - 2:
    while(right < n and nums[right] - nums[left] <= disk):
        right += 1  # 直到找到最右边的下一个位置
    if right - left > 2: # 等于2只有一种可能
        num = right - left - 1
        res += num * (num - 1) // 2
    left += 1
print(res % 99997867)

 

190601 打麻将可以胡的牌

https://www.nowcoder.com/test/question/done?tid=24202466&qid=362292#summary
在这里插入图片描述思路分析:

  1. 可以胡的牌一共有1-9这9种可能性,先判断给定的13张牌里有没有4张一样的,如果有,则证明这张牌已经拿满了,不能再胡这张,可以排除,所以可以胡的牌就是card_list列表里剩下的那些。
  2. 遍历card_list列表,将给定的13张和card_list列表里当前的牌送入helper函数,判断能不能胡,如果能胡,就将当前牌添加到res里,用于最后的输出。
  3. 关于helper函数,第一次调用的时候,传进来是按顺序码好的14张牌,先判断第一张牌有几张,如果大于2张,则可以当做雀头,递归判断nums[2:];如果大于3张,可以当做顺子,递归判断nums[3:];也可以判断nums里是否包含第一张牌在内的ABC型顺子,如果是,则去掉该顺子,递归判断剩下的nums列表。
from collections import Counter

# 判断是否可以胡牌
def helper(nums):
    if not nums:
        return True
    quetou = nums.count(nums[0]) # 先找雀头
    if len(nums) % 3 != 0 and quetou >= 2 and helper(nums[2:]): # len(nums) % 3 != 0条件限制不能出现多个雀头(避免出现AABBCCDD的情况,不符合要求)
        return True
    if quetou >= 3 and helper(nums[3:]): # 移除AAA类型的顺子,看后面的能不能胡牌
        return True

    if nums[0] + 1 in nums and nums[0] + 2 in nums:  # 判断ABC类型的顺子能否胡牌
        tmp_nums = nums.copy()  # 这里一定要用copy函数,不能直接删除nums的元素
        tmp_nums.remove(nums[0])
        tmp_nums.remove(nums[0] + 1)
        tmp_nums.remove(nums[0] + 2)
        if helper(tmp_nums):
            return True
    return False # 以上条件都不满足,则不能胡牌

nums = list(map(int, input().split()))
d = Counter(nums)
res = []
card_list = set(range(1,10)) - {i for i,v in d.items() if v == 4} # 把等于4的牌排除掉,因为手里已经拿四张了,不可能还胡这张牌
for i in card_list: #所有可能胡的牌
    if helper(sorted(nums + [i])): # 如果加入i这张牌能胡,则将i加入res中 【注意】一定要用sort函数排好序再输入helper函数,
        res.append(i)
print(" ".join(str(i) for i in res) if res else 0)

 

190601 特征提取

在这里插入图片描述在这里插入图片描述

n = int(input())
while n > 0:
    m = int(input()) # 代表视频的帧数
    res = 0  # 用来存储最终结果
    d = {}  # 用来存储每组特征连续出现多少次
    while m > 0:
        data = list(map(int, input().split()))
        if data[0] <= 0:
            if d:
                res = max(max(d.values()), res)
                d = {}
            m -= 1
            continue
        i = 1
        while i < len(data):
            d[(data[i], data[i + 1])] = d.get((data[i], data[i + 1]), 0) + 1
            i += 2
        res = max(max(d.values()), res)
        m -= 1
    print(res)
    n -= 1

 

190601 毕业旅行

在这里插入图片描述 

190601 找零

在这里插入图片描述 

190601 机器人跳跃问题

在这里插入图片描述

拼多多2019

3. 两两配对差值最小

给定一个长度为偶数的数组arr,将该数组中的数字两两配对并求和,在这些和中选出最大和最小值,请问该如何两两配对,才能让最大值和最小值的差值最小?

输入描述:
一共2行输入。
第一行为一个整数n,2<=n<=10000, 第二行为n个数,组成目标数组,每个数大于等于2,小于等于100。

输出描述: 输出最小的差值。

输入例子1:
4
2 6 4 3

输出例子1:
1

解法:先对输入数组排序,排序后两头的相加,将结果记录在res中,再对res排序,并取最大值和最小值的差值即可。

n = int(input())
nums = list(map(int, input().split()))
nums.sort()
i, j = 0, len(nums) - 1
res = []
while i < j:
    res.append(nums[i] + nums[j])
    i += 1
    j -= 1
res.sort()
print(res[-1] - res[0])

4. 回合制游戏

你在玩一个回合制角色扮演的游戏。现在你在准备一个策略,以便在最短的回合内击败敌方角色。在战斗开始时,敌人拥有HP格血量。当血量小于等于0时,敌人死去。一个缺乏经验的玩家可能简单地尝试每个回合都攻击。但是你知道辅助技能的重要性。
在你的每个回合开始时你可以选择以下两个动作之一:聚力或者攻击。
聚力会提高你下个回合攻击的伤害。
攻击会对敌人造成一定量的伤害。如果你上个回合使用了聚力,那这次攻击会对敌人造成buffedAttack点伤害。否则,会造成normalAttack点伤害。
给出血量HP和不同攻击的伤害,buffedAttack和normalAttack,返回你能杀死敌人的最小回合数。

输入描述:

第一行是一个数字HP
第二行是一个数字normalAttack
第三行是一个数字buffedAttack
1 <= HP,buffedAttack,normalAttack <= 10^9

输出描述:
输出一个数字表示最小回合数

输入例子1:

13
3
5

输出例子1:

5

解法(数学规律):【参考】
直接用数学规律去分析:
1)如果buffedAttack <= 2 * normalAttack,那么全程使用normalAttack更划算
2)如果buffedAttack > 2 * normalAttack,那么优先使用buffedAttack更划算,如果hp % normalAttack > 0,当余数小于normalAttack,最后一回合用normalAttack就能杀死敌人;如果hp % normalAttack == 0,那么一定全程使用buffedAttack更划算。

import math
hp = int(input())
normal = int(input())
buffed = int(input())
if normal * 2 >= buffed:
    print(math.ceil(hp / normal))
else:
    res = 0
    res += (hp // buffed) * 2
    hp = hp % buffed
    if hp != 0 and hp <= normal:
        res += 1
    elif hp != 0 and hp > normal:
        res += 2
    print(res)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值