926的360两道笔试题有Python代码吗?

第一题跑跑卡丁车的,我用的是最笨的贪心算法,对a*(t^2)排序,然后只通过了18%
n = int(input())
lst = []
for i in range(n):
    tmp = input().split()
    tmp = list(map(int,list(tmp)))
    lst.append(tmp)
induct = []
for i in lst:
    induct.append(i[0]*(i[1]**2))
xu = list(enumerate(induct))
xu.sort(key = lambda x:x[1])
#print(xu)
v = 0
l = 0
while xu:
    tmp = xu.pop()
    a = lst[tmp[0]][0]
    t = lst[tmp[0]][1]
#    print(a,t)
    l += v*t + 0.5*a*t*t
    v += a*t
print(round(l,1))
第二题,看连续漫画的,我用的是最笨的方法,嵌套两层循环,结果超出时间限制,但是如果不计较时间的话,应该是100%。我想知道更简单的方法
lst1 = input().split()
lst1 = list(map(int,list(lst1)))
lst2 = input().split()
lst2 = list(map(int,list(lst2)))
#lst1 = [10,3]
#lst2 = [600,5,2,800,3,5,6,7,8,9]
total_max = 0
for k in range(lst1[1],lst1[0]):
    max_ = sum(lst2[:k])
    tmp = sum(lst2[:k])
    for i in range(k,lst1[0]):
        if tmp + lst2[i] - lst2[i-k] > max_:
            max_ = tmp + lst2[i] - lst2[i-k]
        tmp = tmp + lst2[i] - lst2[i-k]
    average = max_/k
    if average > total_max:
        total_max = average
print('%.3f'%total_max)


#笔试题目##360公司##题解#
全部评论
第一题按 a t的顺序排序贪心,第二题貌似是滑动窗口
点赞
送花
回复
分享
发布于 2019-09-27 02:35

相关推荐

点赞 评论 收藏
转发
点赞 1 评论
分享
牛客网
牛客企业服务