字节跳动春招研发部分编程题汇总

1、万万没想到之聪明的编辑

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

作者:算法才是灵魂
链接:https://www.nowcoder.com/exam/test/81701424/submission?examPageSource=Company&pid=16516564&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fcompany%3FcurrentTab%3Drecommand%26jobId%3D100%26selectStatus%3D0%26tagIds%3D665,134&testclass=%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91
来源:牛客网

2、万万没想到之抓捕孔连顺

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

res = 0
left = 0
right = 2

while left < n-2:
    while right < n and nums[right] - nums[left] <= dist:
        right += 1
    if right - 1 - left >= 2:
        num = right - left - 1
        res += num * (num - 1) // 2
    left += 1

print(res % 99997867)

作者:算法才是灵魂
链接:https://www.nowcoder.com/exam/test/81701424/submission?examPageSource=Company&pid=16516564&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fcompany%3FcurrentTab%3Drecommand%26jobId%3D100%26selectStatus%3D0%26tagIds%3D665,134&testclass=%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91
来源:牛客网

3.雀魂启动!

from collections import Counter

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



def fun(l):
    if len(l) == 0:
        return True
    if len(l) < 3:
        return False
    d = Counter(l)
    ss = set(l)
    for e in ss:
        if d[e] >= 3:
            ll = []
            for k, v in d.items():
                ll.extend([k] * v) if k != e else l.extend([k] * (v - 3))
            if fun(ll):
                return True
        if (e in ss) and (e+1 in ss) and (e + 2 in ss):
            ll = []
            for k, v in d.items():
                ll.extend([k] * v) if k not in (e,e+1,e+2) else ll.extend([k] * (v - 1))
            if fun(ll):
                return True
    return False




d = Counter(nums)


for i in range(1,10):
    if d[i] == 4:
        continue
    d[i] += 1
    sign = False
    ss = set(d.keys())
    for e in ss:
        if d[e] >= 2:
            l = []
            for k,v in d.items():
               l.extend([k] * v) if k != e else l.extend([k] * (v-2))
            if fun(l):
                sign = True
                break
    if sign:
        print(i, end=" ")
    d[i] -= 1

作者:算法才是灵魂
链接:https://www.nowcoder.com/exam/test/81701424/submission?examPageSource=Company&pid=16516564&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fcompany%3FcurrentTab%3Drecommand%26jobId%3D100%26selectStatus%3D0%26tagIds%3D665,134&testclass=%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91
来源:牛客网

4.特征提取

n = int(input())

while n > 0:
    m = int(input())
    res = 1
    d = {}
    for i in range(m):
        l = list(map(int , input().split()))
        k = l[0]
        tmp_d = {}
        for j in range(k):
            index = l[2 * j + 1] * 1000000000 + l[2 * j + 2]
            if index in d:
                tmp_d[index] = d[index] + 1
                res = max(res, tmp_d[index])
            else:
                tmp_d[index] = 1
        d = tmp_d
    print(res)
    n -= 1

作者:算法才是灵魂
链接:https://www.nowcoder.com/exam/test/81701424/submission?examPageSource=Company&pid=16516564&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fcompany%3FcurrentTab%3Drecommand%26jobId%3D100%26selectStatus%3D0%26tagIds%3D665,134&testclass=%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91
来源:牛客网

5、毕业旅行问题

状态DP

n = int(input())
m = []
for i in range(n):
    m.append(list(map(int, input().split())))

V = 1 << (n-1)  #从左至右每一位二进制代表第i个城市是否被访问 如1000代表,第一个城市被访问,而其他城市没有
dp = [[float("inf")] * V for i in range(n)] # dp[i][j]:从节点i只经过集合j所有点再回到0点所需要的最小开销

for i in range(n):
    dp[i][0] = m[i][0]

for j in range(1,V):
    for i in range(n):
        for k in range(1,n):  #能不能先到k城市
            if (j >> (k-1) & 1) == 1: #可以途径k
                dp[i][j] = min(dp[i][j], m[i][k] + dp[k][j ^ (1 << (k-1))])

#从0出发,经过所有点,再回到0的费用
print(dp[0][(1 << (n-1)) - 1])

作者:算法才是灵魂
链接:https://www.nowcoder.com/exam/test/81701424/submission?examPageSource=Company&pid=16516564&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fcompany%3FcurrentTab%3Drecommand%26jobId%3D100%26selectStatus%3D0%26tagIds%3D665,134&testclass=%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91
来源:牛客网

6.找零

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int>coins(4);
    coins[0]=64,coins[1]=16,coins[2]=4,coins[3]=1;
        int num;
        cin>>num;
        int res=0;
        num=1024-num;
        for(int i=0;i<4;i++){
            res+=num/coins[i];
            num=num%coins[i];
            if(num==0){
                break;
            }
        }
        cout<<res<<endl;
    return 0;
}

7.机器人跳跃问题

来自题解评论区

n = int(input())
high = list(map(int, input().split()))

left, right = 0, max(high)

def fun(power):
    for i in range(n):
        if power > high[i]:
            power += power - high[i]
        else:
            power -= high[i] - power
        if power < 0:
            return False
    return True

while left < right:
    mid = (left + right) >> 1
    if fun(mid):
        right = mid
    else:
        left = mid + 1

print(left)

作者:算法才是灵魂
链接:https://www.nowcoder.com/exam/test/81696452/submission?examPageSource=Company&pid=16516564&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fcompany%3FcurrentTab%3Drecommand%26jobId%3D100%26selectStatus%3D0%26tagIds%3D665,134&testclass=%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91
来源:牛客网
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值