2021百度机器学习\NLP笔试编程题
第一题,选礼物(直接贪心算法,ac 100)
n,m,k = [int(i) for i in input().split()] # print(n,m,k) items = [] for _ in range(n): item = [int(i) for i in input().split()] item[0] = 1/item[0] item[1] = 1/item[1] items.append(item) items = sorted(items,key=lambda x: (x[2],x[0],x[1]),reverse=True) # print(items) item_num = 0 for item in items: price = int(1 / item[0]) weight = int(1/item[1]) if k>=price and m>=weight: k = k-price m = m-weight item_num += 1 print(item_num)第二道题,走棋子(动态规划, acc 53)
有一些情况下需要棋子往或者往左走,动态规划不能完全解决,没时间改了....
n = int(input()) values = [ [int(i) for i in input().split()] for _ in range(n)] bp = [[0 for _ in range(n)] for _ in range(n)] # print(values) # print(bp) for i in range(1,n): bp[0][i] = abs(values[0][i]-values[0][i-1])+bp[0][i-1] for i in range(1, n): bp[i][0] = abs(values[i][0]-values[i-1][0])+bp[i-1][0] for i in range(1,n): for j in range(1,n): bp[i][j] = min(abs(values[i-1][j]-values[i][j])+bp[i-1][j],abs(values[i][j-1]-values[i][j])+bp[i][j-1]) # print(bp) print(bp[n-1][n-1])