算法在N组计算的总成本 [英] Algorithm for calculating total cost in groups of N

查看:150
本文介绍了算法在N组计算的总成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有限的对象供应并作为对象购买,价格上升。因此在N(每N个对象都买,价格上涨的时间)组。当试图购买的对象,什么是计算总成本的最简单的方法是什么?

I have a limited supply of objects and as the objects are purchased, the price goes up accordingly in groups of N (every time N objects are bought, price increases). When trying to purchase a number of objects, what is the easiest way to calculate total cost?

例:
我有24个富。对于每一个N(例如使用3)被购买,价格增加1
所以,如果我买1为1的价格则有23个左右,2人给出在1
价格 1已经购买了之后,有人希望购买6井的总成本是=(2 * 1)+(3×2)+(1 * 3)

Example:
I have 24 foo. For every N(example using 3) that are purchased, the price increases by 1.
So if I buy 1 at the prices of 1 then there are 23 left and 2 left at the price of 1.
After 1 has been purchased, someone wishes to buy 6. Well the total cost would be = (2*1)+(3*2)+(1*3)

推荐答案

OK,我认为这是正确的,现在......

OK, I think that this is correct now...

由于:

X  = Total Number of Items Bought
N  = Number of Items per Price Increment
B  = Base Item Price, before any Increments
I  = Price Increment per [N]

设置:

J  = FLOOR((X-1)/N)+1

然后:

TotalCost = X*(B-I) + I*(X*J - N*J*(J-1)/2)

这篇关于算法在N组计算的总成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆