更新两家笔试编程题(华为/360企业安全)

360企业安全笔试

第一题

求一组序列的最大公约数

def gys(a):
    res_a=[]
    for i in range(1,a+1):
        if a%i==0:
            res_a.append(i)
    return res_a
def maxgys(a,b):
    res_a=gys(a)
    res_b=gys(b)
    res=1
    for i in res_a:
        if i in res_b:
            res=max(res,i)
    return res
def findmax(newlist):
    if len(newlist)==2:
        return maxgys(newlist[0],newlist[1])
    a=newlist[0]
    b=newlist[1]
    return findmax([maxgys(a,b)]+newlist[2:])

第二题

给定一个二维数组,输出二维数组的最大上升序列
Input
[

​ [9,9,4],

​ [8,6,9],

​ [2,1,1]

]

Output:
4 note:[1,2,8,9]

def tj(newlist):
    hang=len(newlist)
    lie=len(newlist[0])
    res={}
    for i in range(hang):
        for j in range(lie):
            if newlist[i][j] not in res:
                res[newlist[i][j]]=[[i,j]]
            else:
                res[newlist[i][j]].append([i,j])
    return res

def maxzxl(newlist):
    tongji=tj(newlist)
    values=sorted(list(tongji.keys()))
    hang=len(newlist)
    lie=len(newlist[0])
    mod_newlist=[[-10e9]*(lie+2)]+[[-10e9]+i+[-10e9] for i in newlist]+[[-10e9]*(lie+2)]
    res=[[0 for i in range(lie+2)] for j in range(hang+2)]
    while len(values)>0:
        value=values.pop()
        valuelist=tongji[value]
        for ind in valuelist:
            i=ind[0]
            j=ind[1]
            maxsize=0
            if mod_newlist[i][j+1]>mod_newlist[i+1][j+1]:
                maxsize=max(maxsize,res[i][j+1])
            if mod_newlist[i+2][j+1]>mod_newlist[i+1][j+1]:
                maxsize=max(maxsize,res[i+2][j+1])
            if mod_newlist[i+1][j]>mod_newlist[i+1][j+1]:
                maxsize=max(maxsize,res[i+1][j])
            if mod_newlist[i+1][j+2]>mod_newlist[i+1][j+1]:
                maxsize=max(maxsize,res[i+1][j+2])
            res[i+1][j+1]=maxsize+1
    return max(tj(res).keys())

华为笔试的三个题目

第一题:

输入一个整数数列,6个元素,问能够组成的最大时间,如果不能组成输入invalid

def findk(sortnewlist,k):
    for i in range(len(sortnewlist)-1):
        if sortnewlist[i]<=k and sortnewlist[i+1]>k:
            return i
    if sortnewlist[-1]<=k:
        return len(sortnewlist)-1
    else:
        return None
def maxtime(newlist):
    newlist.sort()
    if newlist[0]>2:
        return 'invalid'
    hour_shi_index=findk(newlist,2)
    if hour_shi_index is None:
        return 'invalid'
    hour_shi=newlist[hour_shi_index]
    newlist = newlist[:hour_shi_index] + newlist[hour_shi_index+1:]
    if hour_shi==2:
        hour_ge_index=findk(newlist,3)
        if hour_ge_index is None:
            return 'invalid'
        hour_ge=newlist[hour_ge_index]
        res=str(hour_shi)+str(hour_ge)+':'
        newlist=newlist[:hour_ge_index]+newlist[hour_ge_index+1:]
    else:
        res=str(hour_shi)+str(newlist[-1])+':'
        newlist=newlist[:-1]
    minute_shi_index=findk(newlist,5)
    if minute_shi_index is None:
        return 'invalid'
    minute_shi=newlist[minute_shi_index]
    newlist=newlist[:minute_shi_index]+newlist[minute_shi_index+1:]
    res=res+str(minute_shi)+str(newlist[-1])+':'
    newlist=newlist[:-1]
    minute_shi_index = findk(newlist, 5)
    if minute_shi_index is None:
        return 'invalid'
    minute_shi = newlist[minute_shi_index]
    newlist = newlist[:minute_shi_index] + newlist[minute_shi_index + 1:]
    res = res + str(minute_shi) + str(newlist[-1])
    newlist = newlist[:-1]
    if len(newlist)!=0:
        return 'invalid'
    return res

第二题:

小明手里有点闲钱,做点卖水果的小买卖,m代表水果的成本价,n代表卖出价,本钱k元,最后最多能赚多少钱?
eg:
m = [4, 2, 6, 4]
n = [5, 3, 11, 7]
k=15
先买0,1,2三种水果,然后卖出,最后买最后一个水果,卖出
思路:背包问题,给出最优的策略,然后将m删除这些元素,n删除这些元素,k表示最终的价钱,重新计算最优,一直到两者相等结束

"""
基础的背包问题,输出需要选择的路线策略
"""
def findroad(weight,value,k):
    res=[[0 for i in range(k+1)] for j in range(len(weight)+1)]
    road=[[[] for i in range(k+1)] for j in range(len(weight)+1)]
    for i in range(1,len(weight)+1):
        for j in range(1,k+1):
            if j>=weight[i-1]:
                if res[i-1][j]>=res[i-1][j-weight[i-1]]+value[i-1]:
                    res[i][j]=res[i-1][j]
                    road[i][j]=road[i-1][j][:]
                else:
                    res[i][j]=res[i-1][j-weight[i-1]]+value[i-1]
                    road[i][j]=road[i-1][j-weight[i-1]][:]
                    road[i][j].append(i-1)
            else:
                res[i][j]=res[i-1][j]
                road[i][j] = road[i - 1][j][:]
    return [res[-1][-1],road[-1][-1]]
def delete(newlist,ind_list):
    res=[]
    for ind,val in enumerate(newlist):
        if ind not in ind_list:
            res.append(val)
    return res
def findmax(weight,value,k):
    resk=findroad(weight,value,k)[0]+k
    resr=findroad(weight,value,k)[-1]
    weight=delete(weight,resr)
    value=delete(value,resr)
    secondk=findroad(weight,value,resk)[0]+resk
    while secondk>resk and len(weight)!=0:
        resr = findroad(weight, value, resk)[-1]
        resk=secondk
        weight=delete(weight,resr)
        value=delete(value,resr)
        secondk=findroad(weight,value,resk)[0]+resk
    return resk

第三题

def calctime(m,n,time):
    if m>n:
        return max(time)
    else:
        sumtime=0
        time.sort()
        time.reverse()
        process=time[-m:]
        time=time[:-m]
        while len(time)>0:
            need=time.pop()
            minut=min(process)
            process=[i-minut for i in process]
            sumtime+=minut
            for i in range(len(process)):
                if process[i]==0:
                    process[i]=need
                    break
        sumtime+=max(process)
        return sumtime

你可能感兴趣的:(笔试编程)