编写代数方程
在另一篇文章中,MSN 为我提供了解决代数问题的良好指南(计算出价总成本)。 现在,尽管我可以手动计算它,但我完全不知道如何用伪代码或代码编写它。 有人可以给我一个快速提示吗? 顺便说一句,我想根据最终成本计算出价。
usage cost(bid) = PIN(bid*0.10, 10, 50)
seller cost(bid) = bid*.02
added cost(bid) = PIN(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10)
storing cost(bid) = 100
So the final cost is something like:
final cost(bid) = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 20) + PIN(ceiling((bid - 1000)/2000)*10, 0, 20) + bid*.02 + 100 + bid
Solve for a particular value and you're done.
For example, if you want the total cost to be $2000:
2000 = PIN(bid*.1, 10, 50) + pin(ceiling(bid/500)*5, 5, 10) + PIN(ceiling((bid - 1000)/2000)*5, 0, 10) + bid*.02 + 100 + bid.
Bid must be at least > 1500 and < 2000, which works out nicely since we can make those PIN sections constant:
2000 = 50 + 10 + 5 + 100 + bid*1.02
1835 = bid*1.02
bid = 1799.0196078431372549019607843137
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该函数简化为:
如果您将每个部分视为一个单独的函数,则它们可以反转:
如果您将成本代入每个函数,您将获得该范围的估计出价。 您必须检查该值是否确实在该函数的有效范围内。
示例:
由于原始函数是严格递增的,因此这些函数中最多有一个会给出可接受的值。 但对于某些输入,它们都不会给出良好的值。 这是因为原始函数跳过了这些值。
因此,没有函数可以给出可接受的值,例如
cost = 1182
。The function simplifies to:
If you consider each piece as a separate function, they can be inverted:
If you plug your cost into each function you get an estimated bid value for that range. You must check that this value indeed is within that functions valid range.
Example:
Since the original function is strictly increasing, at most one of those functions will give an acceptable value. But for some inputs none of them will give a good value. This is because the original function jumps over those values.
So no function will give an acceptable value for
cost = 1182
for example.由于使用了
PIN
和ceiling
,我没有看到一个简单的方法来反转计算。 假设 bid 具有固定精度(我猜点后面有两位小数),您始终可以使用二分搜索(因为函数是单调的)。编辑:经过更多思考,我观察到,采用
x = bid*1.02 + 100
,我们得到最终成本在x+15(不含)和x+70(含)之间(即x+15 <最终成本)。 考虑到此范围的大小 (
70-15=55
) 以及bid
的特殊值(参见下面的注释)的差距都大于此,您可以取x+15 =最终成本
和x+70 =最终成本
,获得正确的使用情况/值和增加的成本,并简单地求解该方程(不再有其中的PIN
或ceiling
)。为了说明这一点,让最终成本为
222
。 从x+15 = 222
得出bid = 107/1.02 = 104.90
。 那么我们可以得到使用成本由bid*0.1
给出,额外成本为5
。 换句话说,我们得到最终成本 = bid*0.1 + bid*0.02 + 5 + 100 + bid = bid*1.12 + 105
,因此bid = (222-105)/1.12 = 104.46。 由于此
bid
值意味着采用了正确的使用值和额外费用,因此我们知道这就是解决方案。但是,如果我们首先查看 x+70 = 222,我们会得到以下结果。 首先,我们得出这个假设
bid = 52/1.02 = 50.98
。 这意味着使用成本为10
,额外成本为5
。 因此,我们得到最终成本 = 10 + bid*0.02 + 5 + 100 + bid = bid*1.02 + 115
,因此bid = (222-115)/1.02 = 104.90
。 但如果bid
为104.90
则使用成本不是10
而是bid*0.1
,因此这不是正确的解决方案。我希望我解释得足够清楚。 如果没有,请告诉我。
注意:对于特殊值,我指的是那些定义使用值和附加成本的函数发生变化的值。 例如,对于使用成本,这些值为
100
和500
:低于100
使用10
,高于100
使用10
>500 您使用50
,中间您使用bid*0.1
。Due to the use of
PIN
andceiling
, I don't see a easy way to invert the calculation. Assuming thatbid
has a fixed precision (I'd guess two decimals behind the dot) you can always use a binary search (as the functions are monotone).Edit: After thinking about it some more, I observed that, taking
x = bid*1.02 + 100
, we have that the final costs are between x+15 (exclusive) and x+70 (inclusive) (i.e.x+15 < final cost < x+70
). Given the size of this range (70-15=55
) and the fact that the special values (see note below) forbid
are all apart more than this, you can takex+15 = final cost
andx+70 = final cost
, get the right cases/values of usage and added costs and simply solve that equation (which no longer has eitherPIN
orceiling
in it).To illustrate, let the final cost be
222
. Fromx+15 = 222
it follows thatbid = 107/1.02 = 104.90
. Then we have that the usage costs are given bybid*0.1
and that the additional costs are5
. In other words, we getfinal cost = bid*0.1 + bid*0.02 + 5 + 100 + bid = bid*1.12 + 105
and thereforebid = (222-105)/1.12 = 104.46
. As this value ofbid
means the right values for usage and additional costs were taken, we know that this is the solution.However, if we would have first looked at
x+70 = 222
, we would get the following. First we get that for this assumption thatbid = 52/1.02 = 50.98
. This means that usage costs are10
and the additional costs are5
. So we getfinal costs = 10 + bid*0.02 + 5 + 100 + bid = bid*1.02 + 115
and thereforebid = (222-115)/1.02 = 104.90
. But ifbid
is104.90
then the usage costs are not10
butbid*0.1
, so this isn't the right solution.I hope I explained it clearly enough. If not, please let me know.
N.B.: With special values I mean those for which the function defining the values of usage and added costs change. For example, for usage cost these values are
100
and500
: below100
you use10
, above500
you use50
and in between you usebid*0.1
.