1、求最大利润
已知客户们的出价分别为【2,3,4,6,5,7,3,5】,商品成本为3、规则为当售价小等于客户的出价时,商品可售出、此刻求出最大利润。
python
def getMAXPrift(prices, cost_price):
sorted(prices)
prifts = []
for i, v in enumerate(prices):
if v > cost_price:
prifts.append((v - cost_price) * (len(prices) - i))
return sorted(prifts)[-1]
if __name__ == '__main__':
a = [2, 3, 5, 4, 6, 7, 5]
prift = getMAXPrift(a, 3)
print(prift)
go
package main
import "sort"
// 贪心算法 产品价格三元、按出价人出价售出求最大利润。出价人价格为【2,3,5,6,3,6,7,5】
import "fmt"
func getMAXProfit(prices []int, costPrice int) int {
sort.Ints(prices)
profits := []int{}
for i,v := range prices{
if v > costPrice{
profits = append(profits,(v-costPrice)*(len(prices)-i))
}
}
sort.Ints(profits)
return profits[len(profits)-1]
}
func main() {
priceList := []int{2, 3, 5, 6, 3, 6, 7, 5}
fmt.Println(getMAXProfit(priceList, 3))
}
网友评论