从渐进成本算法中排除循环
Exclude loop from progressive cost algorithm
我有一个算法来计算目标成本的产品数量。一种产品的成本不是恒定的,它会贵 10%。如果目标成本比不完全成本更接近,则可能会超出目标成本。
我需要优化代码并排除循环,因为它在高处无效
目标成本。
这是我的单元测试解决方案(nunit 框架)
public static int CalculateProductAmountOnCost( double targetCost, double productRate )
{
if ( targetCost <= 0 || productRate <= 0 )
return 1;
var previousCollectedCost = 0.0;
var collectedCost = 0.0;
var amount = 0;
for ( ; collectedCost < targetCost; amount++ )
{
previousCollectedCost = collectedCost;
collectedCost += productRate*Math.Pow( 1.1, amount );
}
if ( targetCost - previousCollectedCost < collectedCost - targetCost )
amount -= 1;
return Math.Max( 1, amount );
}
[Test]
[TestCase( 9, 2, 4 )]
[TestCase( 7, 2, 3 )]
[TestCase( 0, 2, 1 )]
[TestCase( 9, 0, 1 )]
[TestCase( 4.5, 0.2, 12 )]
public void TradeDealHelper_CalculateProductAmountOnGemCostTest( double targetCost, double productRate,
int expectedAmount )
{
var actualAmount = TradeDealHelper.CalculateProductAmountOnCost( targetCost, productRate );
Assert.AreEqual( expectedAmount, actualAmount );
}
public static int CalculateProductAmountOnCost2(double targetCost, double productRate)
{
if (targetCost <= 0 || productRate <= 0)
return 1;
var amount = (int)Math.Floor(Math.Log(((targetCost / (productRate * 10)) + 1), 1.1) - 1);
var lowerCost = productRate * 10 * (Math.Pow(1.1, amount + 1) - 1);
var upperCost = productRate * 10 * (Math.Pow(1.1, amount + 2) - 1);
if (targetCost - lowerCost > upperCost - targetCost)
amount++;
return Math.Max(1, amount + 1);
}
您可以使用一些数学知识。下面基本上是数学的东西,不是代码。
您的总费用是:
totalCost = productRate + productRate * 1.1 + productRate * (1.1 ^ 2) + ... + productRate * (1.1 ^ n)
可以改写为:
totalCost = productRate * (1 + 1.1 + 1.1 ^ 2 + ... 1.1 ^ n)
有一个计算总和的公式:
1 + a + a ^ 2 + ... a ^ n = (a ^ (n + 1) - 1) / (a - 1)
所以你现在的总费用是:
totalCost = productRate * ((1.1 ^ (n + 1) - 1) / (1.1 - 1))
totalCost = productRate * 10 * (1.1 ^ (n + 1) - 1)
我们应用最后一个公式为您的 targetCost
求出 n
,我们得到:
n = Math.Log((targetCost / (productRate * 10)) + 1, 1.1) - 1
(以 1.1 为底数的对数)
代码正在应用这些公式。
我有一个算法来计算目标成本的产品数量。一种产品的成本不是恒定的,它会贵 10%。如果目标成本比不完全成本更接近,则可能会超出目标成本。
我需要优化代码并排除循环,因为它在高处无效 目标成本。
这是我的单元测试解决方案(nunit 框架)
public static int CalculateProductAmountOnCost( double targetCost, double productRate )
{
if ( targetCost <= 0 || productRate <= 0 )
return 1;
var previousCollectedCost = 0.0;
var collectedCost = 0.0;
var amount = 0;
for ( ; collectedCost < targetCost; amount++ )
{
previousCollectedCost = collectedCost;
collectedCost += productRate*Math.Pow( 1.1, amount );
}
if ( targetCost - previousCollectedCost < collectedCost - targetCost )
amount -= 1;
return Math.Max( 1, amount );
}
[Test]
[TestCase( 9, 2, 4 )]
[TestCase( 7, 2, 3 )]
[TestCase( 0, 2, 1 )]
[TestCase( 9, 0, 1 )]
[TestCase( 4.5, 0.2, 12 )]
public void TradeDealHelper_CalculateProductAmountOnGemCostTest( double targetCost, double productRate,
int expectedAmount )
{
var actualAmount = TradeDealHelper.CalculateProductAmountOnCost( targetCost, productRate );
Assert.AreEqual( expectedAmount, actualAmount );
}
public static int CalculateProductAmountOnCost2(double targetCost, double productRate)
{
if (targetCost <= 0 || productRate <= 0)
return 1;
var amount = (int)Math.Floor(Math.Log(((targetCost / (productRate * 10)) + 1), 1.1) - 1);
var lowerCost = productRate * 10 * (Math.Pow(1.1, amount + 1) - 1);
var upperCost = productRate * 10 * (Math.Pow(1.1, amount + 2) - 1);
if (targetCost - lowerCost > upperCost - targetCost)
amount++;
return Math.Max(1, amount + 1);
}
您可以使用一些数学知识。下面基本上是数学的东西,不是代码。
您的总费用是:
totalCost = productRate + productRate * 1.1 + productRate * (1.1 ^ 2) + ... + productRate * (1.1 ^ n)
可以改写为:
totalCost = productRate * (1 + 1.1 + 1.1 ^ 2 + ... 1.1 ^ n)
有一个计算总和的公式:
1 + a + a ^ 2 + ... a ^ n = (a ^ (n + 1) - 1) / (a - 1)
所以你现在的总费用是:
totalCost = productRate * ((1.1 ^ (n + 1) - 1) / (1.1 - 1))
totalCost = productRate * 10 * (1.1 ^ (n + 1) - 1)
我们应用最后一个公式为您的 targetCost
求出 n
,我们得到:
n = Math.Log((targetCost / (productRate * 10)) + 1, 1.1) - 1
(以 1.1 为底数的对数)
代码正在应用这些公式。