[英]Deriving buy 3 get 1 free formula
我想得出買3送1的免費公式,例如:
Buy 1, pay price of 1
Buy 2, pay price of 2
Buy 3, pay price of 2
Buy 4, pay price of 3
Buy 5, pay price of 4
Buy 6, pay price of 4
看起來像這樣:
1 - 0
2 - 0
3 - 1
4 - 1
5 - 1
6 - 2
7 - 2
8 - 2
9 - 3
依此類推,但我無法將其公式化為n-1等等。
有沒有人可以參考我的參考資料?
totalPrice = Math.ceil(totalItems / 3 * 2);
每3件商品您要支付2的價格。如果您多買了1-2件商品,則要全額支付。
totalPrice = (totalItems/3)*2+ totalItems%3
假設totalItems保存為整數,這應該可以工作。
假設項目數為N,則可寫為:
N = 3 * M + L
(L <3)
M是整數除法的結果-數學上除法的底限-
L是整數除法的余數。
價錢是:
P = 2 * M + L
您在第二張表中顯示的差異恰好是
totalItems/3
所以要支付的物品是
totalItems-totalItems/3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.