采購單(京東2017秋招真題)
阿新 • • 發佈:2017-05-16
python 格式
本來也不算很難的一道題,但是總是只通過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%
類似非空的語句,以前我總不寫,這題遇到好多類似的麻煩,也沒完全搞懂,可以考慮當個模板吧... ...
本文出自 “12242293” 博客,請務必保留此出處http://12252293.blog.51cto.com/12242293/1926031
采購單(京東2017秋招真題)