本来也不算很难的一道题,但是总是只通过60% 或者 80%
一眼就看出思路,代码也不难的题就是不能全部通过也是神烦,应该是格式上的问题,也不算完全弄明白了,简单说说,作为一种经验。
思路:给价钱 和 每种物品的个数排序
最贵:最高价买最多的物品,累加
最低:最低价买最多的物品。累加
语句:list.count() list.sort() set(list)等
以下为正确的python语句
while 1:
r = raw_input()
if r != '':
(n,m) = (int(x) for x in r.split())
price = [int(x) for x in raw_input().split()]
wishlist = []
iter = 0
while iter <m:
want = raw_input()
if want != '':
wishlist += want.split()
iter +=1
number = [wishlist.count(x) for x in list(set(wishlist))]
price.sort()
number.sort(reverse = True)
min = [x*y for x,y in zip(price,number)]
cost_min = sum(min)
max = [x*y for x,y in zip(price[::-1],number)]
cost_max = sum(max)
print (str(cost_min) + ' ' +str(cost_max))
注意以下问题:
注意第9和11行,使用split(),如果交换,就只能通过80%了,感觉是和第10行的判断非空语句有关
写iter循环使用的是while循环,我前几遍写的时候是用for循环,一直只能通过80%,感觉还是和第10行有关,while循环是在判断非空之后添加的,而for循环是一开始就有的,可能是这里的问题吧??这一点没有搞清楚... ...
第三行要写r的非空判断,不然一直只通过60%
类似非空的语句,以前我总不写,这题遇到好多类似的麻烦,也没完全搞懂,可以考虑当个模板吧... ...
转载于:https://blog.51cto.com/12252293/1926031