第一題的思路很簡單,分爲兩步:
1、根據0/1判斷是否爲打折的商品,然後打折與不打折總和
2、使用優惠券的情況下,應該考慮m種,然後比較大小
最後兩個方案得出最小价格。
有點坑的地方是,不能使用庫,不能使用庫,不能使用庫!
題主開始用numpy一直出錯,語法錯誤或越界訪問。
n,m = (int(x) for x in input().strip().split())
a = []
b = []
for i in range(n):
l1 = [int(x) for x in input().strip().split()]
a.append(l1)
for i in range(m):
l2 = [int(x) for x in input().strip().split()]
b.append(l2)
# 選擇優惠券
discount = 0.0 # 打折商品的價格
undiscount = 0.0 #不打折商品的價格
for d in range(n):
if a[d][1] == 1: #若等於1,則該商品打八折
discount8 = float(a[d][0]) * 0.8
discount += discount8
else:
undiscount += float(a[d][0])
discount_total = discount + undiscount
# 選擇減滿
total = 0.0
for i in range(n):
total += float(a[i][0])
cost = []
for i in range(m):
if total >= float(b[i][0]):
value = total - float(b[i][1])
cost.append(value)
else:
cost.append(total)
mincost = min(cost)
minvalue = 0.0
if discount_total > mincost:
minvalue = mincost
else:
minvalue = discount_total
print('%.2f' %minvalue)
最後結果通過: