vivo2020校招-软件开发类-编程题

vivo有3道编程题,一个小时的笔试时间。一个小时3道题,已经决定了vivo的笔试难度不高

按往常惯例就是第一题送分、第二题送点分、第三题稍微靠实力拿分。


第一题(3分钟题 AC)

给一个字符串,()代表一个礼物盒,礼物盒中叠加礼物盒,问至少打开多少层礼物盒,可以拿到礼物,’0‘代表礼物。其实很容易就可以发现,我们只需要看0之前有多少个’( ’,减去有多少个 ’)‘,答案就出来了。

输入:
(()(()((()(0)))))
输出:
5
输入:
(((0)))
输出:
3
def solution(s):
    # TODO Write your code here
    # print(s.index('0'))
    key = s.index('0')
    a = 0
    b = 0
    for i in s[:key]:
        if i == '(':
            a+=1
        else:
            b+=1
    return a-b


if __name__ == '__main__':
    input = input()
    print(solution(input))

第二题(常规深搜题 只要控制好深搜条件即可 AC)

题目忘了差不多,就不说了

max_value = -1

def lengh(total_disk,total_memory,now_list,disk,memory,value):
    global max_value
    # print(value)
    if disk > total_disk or memory > total_memory:
        return
    if value>max_value:
        max_value = value
    if len(now_list)==0:
        return
    key = now_list.pop()
    lengh(total_disk,total_memory,now_list.copy(),disk,memory,value)
    lengh(total_disk, total_memory, now_list.copy(), disk+key[0], memory+key[1], value+key[2])

def solution(total_disk, total_memory, app_list):
    # TODO Write your code here
    lengh(total_disk,total_memory,app_list,0,0,0)
    return max_value


if __name__ == "__main__":
    input1 = input()
    disk = int(input1.split()[0])
    memory = int(input1.split()[1])
    input2 = input1.split()[2]
    app_list = [[int(j) for j in i.split(',')] for i in input2.split('#')]
    # print(app_list)
    print(solution(disk, memory, app_list))

 


第三题(笔试太久才来写 已经忘了题目 反正是一道动态规划题 但是可能还要进行一些优化,当时怎么修改都只能到70%)

'''
Welcome to vivo !
'''
max_score = 0
# 执行消除动作
def dele(list,inde):
    # 这里可能会有越界问题
    num = list[inde]
    k =1
    for i in range(inde+1,len(list)):
        if list[i]!=num:
            break
        else:
            k+=1
    res = list[:inde]+list[inde+k:]
    return (res,k*k)

# 分解出当前的所有可执行的情况,以及对应增加的分数
def kill(list):
    res = []    # [([执行后的数组],增加的分数int),()]
    key = True
    num = 0

    for i in range(1,len(list)):
        if list[i] == list[i-1] and key:
            res.append(dele(list,i-1))
            key = False
            num = list[i]
        elif list[i]!=num:
            key = True

    if len(res)==0 and len(list)!=0:
        for i in range(len(list)):
            res.append((list[:i] + list[i + 1:], 1))
        # res.append((list[1:], 1))

    # print(res)
    return res

def lengh(nowlist,score):
    global max_score
    if len(nowlist)==0:
        if score>max_score:
            max_score=score
        return
    for i in kill(nowlist):
        lengh(i[0],score+i[1])
    return

def solution(boxes):
    # TODO Write your code here
    lengh(boxes, 0)
    return max_score

if __name__ == '__main__':
    x = input()
    boxes = list(map(int, x.split()))
    # # print(boxes)
    print(solution(boxes))

 

你可能感兴趣的:(题库)