生活随笔
收集整理的這篇文章主要介紹了
1020 月饼 (25分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入樣例:
3 20
18 15 10
75 72 45
輸出樣例:
94.50
解題心得:
本題因為要排序,優先考慮使用二維數組和sorted最常用的模式;python在建立二維數組時有一個坑,就是[[]] * n中出現淺拷貝問題,操縱一個元素等于操作所有元素,正確建立二維數組的方式是
for i
in range(n
):cakes
.append
([])
注意審題,哪些是正整數,哪些是正數,pta平臺返回三大類錯誤:1??答案錯誤,那就是語法沒問題是邏輯的問題;2??沒有得到返回,語法錯誤,這時候就要考慮越界、轉換問題;3??超時問題,主要優化for循環;
import sys
if __name__
== '__main__':input_str
= sys
.stdin
.readline
().split
()n
, d
= int(input_str
[0]), int(input_str
[1])store_str
= sys
.stdin
.readline
().split
()sale_str
= sys
.stdin
.readline
().split
()store_int
= list(map(float, store_str
))sale_int
= list(map(float, sale_str
))cakes
= []for i
in range(n
):cakes
.append
([])for i
in range(n
):price
= sale_int
[i
] / store_int
[i
]cakes
[i
] += [price
, store_int
[i
], sale_int
[i
]]sorted_price
= sorted(cakes
, key
=lambda x
: x
[0], reverse
=True)profit
= 0.0 idx
= 0while d
> 0 and idx
< n
:if d
> sorted_price
[idx
][1]:profit
+= sorted_price
[idx
][2]d
-= sorted_price
[idx
][1]else:profit
+= d
* sorted_price
[idx
][0]breakidx
+= 1print('%.2f' % profit
)
總結
以上是生活随笔為你收集整理的1020 月饼 (25分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。