谁还能想到现在还有华为机考呢,附上答案

第一题,100分,一个处理oj的简单题
扑克牌整理
def func():
    n = int(input())
    all_key = dict()
    for i in range(n):
        num, key = map(int,input().split())
        if key not in all_key:
            all_key[key] = [num]
        else:
            all_key[key].append(num)
            
    boom = []
    t = []
    o = []
    for key, value in all_key.items():
        if len(value) == 4:
            boom.append(key)
        elif len(value) in [2,3]:
            value.sort()
            t.append([key, value[0],value[1]])
            if len(value) == 3:
                o.append([key, value[-1]])
        else:
            o.append([key, value[0]])
        
    # print(all_key)
    if boom:
        boom.sort()
        for k in boom:
            for i in range(1, 5):
                print(i, k)

    if t:
        t.sort()
        for l in t:
            print(l[1], l[0])
            print(l[2],l[0])


    # print(boom, t,o)
    if o:
        o.sort()
        for l in o:
            print(l[1],l[0])

    
if __name__ == "__main__":
    func()
第二题,200分,一个地图行走的,需要判断环,避免重复计算

def func():
    n,m = map(int,input().split())
    maze = []
    for i in range(n):
        maze += [list(input())]
    x_init,y_init = map(int, input().split())

    flag_map = [[0 for j in range(m)] for i in range(n)]

    k = int(input())

    dic = {}

    for i in range(k):
        point = maze[x_init][y_init]
        x_d,y_d = 0,0
        if point=='.':
            return '{} {}'.format(x_init, y_init)
        if flag_map[x_init][y_init]==0:
            # 没来过

            if point=='^':
                x_d -= 1
            if point=='v':
                x_d += 1
            if point=='<':
                y_d-=1
            if point=='>':
                y_d += 1
            flag_map[x_init][y_init]= i
            dic[i]=[x_init][y_init] 
        else:
            loop_num = i-flag_map[x_init][y_init]
            last_index = (k-i+1)%loop_num
            ans = dic[last_index]
            return '{} {}'.format(ans[0], ans[1])

        if x_init+x_d>=n&nbs***bsp;y_init+y_d>=m&nbs***bsp;x_init+x_d<0&nbs***bsp;y_init+y_d<0&nbs***bsp;maze[
                        x_init+x_d][y_init+y_d]=='#':
            return '{} {}'.format(x_init, y_init)

        x_init += x_d
        y_init += y_d
    return '{} {}'.format(x_init, y_init)


   
if __name__ == "__main__":
    print(func())





第三题,300分,dfs,通讯录查找
names = 'zhang guo,zhai an guo,zhai guo,zhai hai guo'
n = 'zhag'
names = names.split(',')


def if_true(words, n):
    if '' in words:
        return False
    if len(words)>len(n):
        return False
    
    if len(words)==len(n):
        r = ''
        for w in words:
            r += w[0]
        return r==n
    
    for i in range(len(words)):
        if words[i][0] in n:
            d = words[i][0]
            j = n.index(d)
            words[i] = words[i][1:]
            if if_true(words, n[:j]+n[j+1:]):
                return True
        
            words[i] = d + words[i]

    return False

names = names.split(',')
res = []
for name in names:
    if name[0] != n[0]:
        continue
    words = name.split()
    # min_name = min([len(w) for w in words])
    if if_true(words, n):
        res.append(name)
print(','.join(res))
words = names[-1].split()
print(if_true(words, n))






#笔试题目#
全部评论
2022 5月18日机考。。
3 回复 分享
发布于 2022-05-19 09:58

相关推荐

点赞 评论 收藏
分享
评论
4
32
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务