買書問題

來源:互聯網
上載者:User

在節假日的時候,書店一般都會做促銷活動。由於《哈利傳輸速率》系列相當暢銷,店長決定通過促銷活動來回饋讀者。在銷售的《哈利傳輸速率》平裝本系列中,一共有五卷,用編號0, 1, 2, 3, 4來表示。假設每一卷單獨銷售均需要8歐元。如果讀者一次購買不同的兩卷,就可以扣除5%的費用,三卷則更多。假設具體折扣的情況如下:

 

本數

折扣

2

5%

3

10%

4

20%

5

25%

 

 

在一份訂單中,根據購買的卷數以及本書,就會出現可以應用不同折扣規則的情況。但是,一本書只會應用一個折扣規則。比如,讀者一共買了兩本卷一,一本卷二。那麼,可以享受到5%的折扣。另外一本卷一則不能享受折扣。如果有多種折扣,希望能夠計算出的總額儘可能的低。

要求根據這樣的需求,設計出演算法,能夠計算出讀者所購買一批書的最低價格。

#! /usr/bin/python# coding=utf-8import randomfrom itertools import groupbydef main():    arr = [random.randint(0,4) for i in range(random.randint(1,10)) ]    print arr    discount(arr)    def discount(arr):    arr =sorted(arr)    data = [len(list(g)) for k,g in groupby(arr)]    table = [0,5,10,20,25]    total = 0    for i in range(max(data)):        data = [i-1 for i in data if i]        l = len(data)        price = l * 8 * (100 - table[len(data) - 1]) / 100        print "%s,%s * 8 - %s%% = %s" % (data,l, table[len(data) - 1], price)        total += price    print total        if __name__ == '__main__':    main()        

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.