编写代数方程

发布于 2024-07-15 04:15:47 字数 1032 浏览 11 评论 0 原文

在另一篇文章中,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

in another post, MSN gave me a good guide on solving my algebra problem (Calculating bid price from total cost). Now, even though I can calculate it by hand, I'm completely stuck on how to write this in pseudocode or code. Anyone could give me a quick hint? By the way, I want to calculate the bid given the final costs .

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‖放下 2024-07-22 04:15:47

该函数简化为:

                  / 1.02 * bid + 115   bid <   100
                  | 1.12 * bid + 105   bid <=  500
final cost(bid) = | 1.02 * bid + 160   bid <= 1000
                  | 1.02 * bid + 165   bid <= 3000
                  \ 1.02 * bid + 170   otherwise

如果您将每个部分视为一个单独的函数,则它们可以反转:

bid_a(cost) = (cost - 115) / 1.02
bid_b(cost) = (cost - 105) / 1.12
bid_c(cost) = (cost - 160) / 1.02
bid_d(cost) = (cost - 165) / 1.02
bid_e(cost) = (cost - 170) / 1.02

如果您将成本代入每个函数,您将获得该范围的估计出价。 您必须检查该值是否确实在该函数的有效范围内。

示例:

cost = 2000

bid_a(2000) = (2000 - 115) / 1.02 = 1848  Too big! Need to be < 100
bid_b(2000) = (2000 - 105) / 1.12 = 1692  Too big! Need to be <= 500
bid_c(2000) = (2000 - 160) / 1.02 = 1804  Too big! Need to be <= 1000
bid_d(2000) = (2000 - 165) / 1.02 = 1799  Good. It is <= 3000
bid_e(2000) = (2000 - 170) / 1.02 = 1794  Too small! Need to be > 3000

Just to check:

final cost(1799) = 1.02 * 1799 + 165 = 2000   Good!

由于原始函数是严格递增的,因此这些函数中最多有一个会给出可接受的值。 但对于某些输入,它们都不会给出良好的值。 这是因为原始函数跳过了这些值。

final cost(1000) = 1.02 * 1000 + 160 = 1180
final cost(1001) = 1.02 * 1001 + 165 = 1186

因此,没有函数可以给出可接受的值,例如 cost = 1182

The function simplifies to:

                  / 1.02 * bid + 115   bid <   100
                  | 1.12 * bid + 105   bid <=  500
final cost(bid) = | 1.02 * bid + 160   bid <= 1000
                  | 1.02 * bid + 165   bid <= 3000
                  \ 1.02 * bid + 170   otherwise

If you consider each piece as a separate function, they can be inverted:

bid_a(cost) = (cost - 115) / 1.02
bid_b(cost) = (cost - 105) / 1.12
bid_c(cost) = (cost - 160) / 1.02
bid_d(cost) = (cost - 165) / 1.02
bid_e(cost) = (cost - 170) / 1.02

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:

cost = 2000

bid_a(2000) = (2000 - 115) / 1.02 = 1848  Too big! Need to be < 100
bid_b(2000) = (2000 - 105) / 1.12 = 1692  Too big! Need to be <= 500
bid_c(2000) = (2000 - 160) / 1.02 = 1804  Too big! Need to be <= 1000
bid_d(2000) = (2000 - 165) / 1.02 = 1799  Good. It is <= 3000
bid_e(2000) = (2000 - 170) / 1.02 = 1794  Too small! Need to be > 3000

Just to check:

final cost(1799) = 1.02 * 1799 + 165 = 2000   Good!

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.

final cost(1000) = 1.02 * 1000 + 160 = 1180
final cost(1001) = 1.02 * 1001 + 165 = 1186

So no function will give an acceptable value for cost = 1182 for example.

葬﹪忆之殇 2024-07-22 04:15:47

由于使用了PINceiling,我没有看到一个简单的方法来反转计算。 假设 bid 具有固定精度(我猜点后面有两位小数),您始终可以使用二分搜索(因为函数是单调的)。

编辑:经过更多思考,我观察到,采用x = bid*1.02 + 100,我们得到最终成本在x+15(不含)和x+70(含)之间(即x+15 <最终成本)。 考虑到此范围的大小 (70-15=55) 以及 bid 的特殊值(参见下面的注释)的差距都大于此,您可以取x+15 =最终成本x+70 =最终成本,获得正确的使用情况/值和增加的成本,并简单地求解该方程(不再有其中的 PINceiling)。

为了说明这一点,让最终成本为 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 。 但如果 bid104.90 则使用成本不是 10 而是 bid*0.1,因此这不是正确的解决方案。

我希望我解释得足够清楚。 如果没有,请告诉我。

注意:对于特殊值,我指的是那些定义使用值和附加成本的函数发生变化的值。 例如,对于使用成本,这些值为 100500:低于 100 使用 10,高于 100 使用 10 >500 您使用 50,中间您使用 bid*0.1

Due to the use of PIN and ceiling, I don't see a easy way to invert the calculation. Assuming that bid 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) for bid are all apart more than this, you can take x+15 = final cost and x+70 = final cost, get the right cases/values of usage and added costs and simply solve that equation (which no longer has either PIN or ceiling in it).

To illustrate, let the final cost be 222. From x+15 = 222 it follows that bid = 107/1.02 = 104.90. Then we have that the usage costs are given by bid*0.1 and that the additional costs are 5. In other words, we get final cost = bid*0.1 + bid*0.02 + 5 + 100 + bid = bid*1.12 + 105 and therefore bid = (222-105)/1.12 = 104.46. As this value of bid 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 that bid = 52/1.02 = 50.98. This means that usage costs are 10 and the additional costs are 5. So we get final costs = 10 + bid*0.02 + 5 + 100 + bid = bid*1.02 + 115 and therefore bid = (222-115)/1.02 = 104.90. But if bid is 104.90 then the usage costs are not 10 but bid*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 and 500: below 100 you use 10, above 500 you use 50 and in between you use bid*0.1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文