如何限制2十进制变量转换为第三个变量,反之亦然? [英] How to convert 2 restricted decimal variables to a third variable and vice versa?
问题描述
我有如下2转换器的方法:
I have 2 convertor methods as below:
private const decimal MaxValidValue = 99.99m;
public decimal ConvertABToC(decimal a, decimal b)
{
return a * b;
}
public void ConvertCtoAB(decimal c, ref decimal a, ref decimal b)
{
if (c > MaxValidValue*MaxValidValue)
{
throw new ApplicationException();
}
if (c <= MaxValidValue)
{
a = 1.00m;
b = c;
}
else
{
// need to introduce some logic or assumptions here
}
}
有知道的3个重要的事情:
There are 3 important things to know:
1)的a和b的变量是在0.00到99.99的范围内,因此C不能有一个大于99.99 * 99.99
1) The a and b variables are in the range of 0.00 to 99.99 therefore c can't have a value greater than 99.99*99.99
2)的a,b和c不能有多于2小数precession如A = 99.123将是无效的。
2) the a, b and c must not have more than 2 decimal precession e.g. a = 99.123 would be invalid.
3),可以使用四舍五入,如果你需要,只要decimal.Round(A * B,2)==℃。
3) you can use rounding if you'd need to as long as decimal.Round(a * b, 2) == c.
4)的组合等(1,3),(3,1),(2,2),(1,4),(0.5,8)或甚至(0.25,16)都是有效的;不要紧,只要为c。将a和b的产物。
4) combinations like (1, 3), (3, 1), (2, 2), (1, 4), (0.5, 8) or even (0.25, 16) are all valid; it doesn't matter as long as c would be the product of a and b.
你将如何完成ConvertCtoAB执行?
How would you complete the implementation of ConvertCtoAB?
非常感谢,
推荐答案
乘 C
10000。然后因素这个数字到它的首要因素。然后找到的质因子的分隔成两个集合,使得在每一组数的乘积小于10,000。如果这样的分区可以发现,然后返回这两款产品由100分为 A
和 B
。否则,添加一个数字,然后重试。
Multiply C
by 10,000. Then factor this number into its prime factors. Then find a partition of the prime factors into two sets such that the product of the numbers in each set is less than 10,000. If such a partition can be found, then return these two products divided by 100 as A
and B
. Otherwise, add one to the number and try again.
例如,如果 C = 100.07
,然后因素是 2,2,5,5,10007
。由于产品之一必须包括系数 10007
,这是一个素数,第一个条件就决不会满意。因此,我们尝试再次 1000701 = 3 * 3 * 3 * 13 * 2851
。这个时候,我们可以分区的数量,我们有 A = 3.51
和 B = 28.51
作为一个可能的解决方案。
For example, if C=100.07
, then the factors are 2, 2, 5, 5, 10007
. Because one of the products must include the factor 10007
, which is a prime number, the first condition can never be satisfied. So we try again with 1000701 = 3*3*3*13*2851
. This time, we can partition the number, and we have A=3.51
and B=28.51
as a possible solution.
您可以最多99次做到这一点。如果你需要100以上,比不已经产生,从 ConvertABToC
的输入值。
You can do this at most 99 times. If you need 100 or more, than the input value cannot have been generated from ConvertABToC
.
这不仅确保了 ConvertCtoAB
的结果,反馈到 ConvertABtoC时
将产生相同的 C
,而不是其他的方式。这似乎违反规则#3,但随后在其他地方的问题是关于四舍五入。
This only guarantees that the result of ConvertCtoAB
, when fed back into ConvertABtoC
will produce the same C
, not the other way around. It appears to violate rule #3, but then elsewhere the question is about rounding.
如果没有舍入都被允许,那么应该停止,并试图在原有的10000 * C后报告不可行。
If no rounding at all is allowed, then one should stop and report infeasibility after trying the original 10000*C.
这篇关于如何限制2十进制变量转换为第三个变量,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!