伪代码:递归地计算总数

时间:2010-03-04 18:32:40

标签: c# programming-languages functional-programming pseudocode

我已经获得了在应用程序中编写逻辑的要求(至少对我有挑战性)。我要写一个业务逻辑,它应该执行以下功能

Total current consumption = current from A elements + current from B elements.
A and B are different types of devices

现在假设提供当前(A + B)所需的电池为'X'

此外,每个X都可以影响总电流消耗,因此我需要再次计算总电流消耗,包括电池电流消耗的第一步

`Total current consumed : A + B + X"`  
where X" is the current consumption of the battery 

现在我应该再次计算所需的电池。我们这样说是Y

提供A + B + X“我们需要Y个电池。

Now check whether X == Y ?
If same, then return Y and exit 
else add more X to the sum (A + B  + X") till X == Y

任何人都可以帮助我使用初始伪代码吗? 任何建议也表示赞赏

Yes the end result this logic should return is number of batteries required. However it should return this result only after computing the total current consumption recursively till X == Y, where 
A : total current consumption of some active elements in a system.
B : total current consumption of some passive elements in a system

Total current consumption is A + B
to supply current of (A+B) amperes i require 'X' no. of batteries.
However each battery also adds some delta amount of current to the total value i.e 
A + B + X"
if the batteries required to supply this delta is still 'X', then return X as the end result, else add more batteries --> calculate current --> no of batteries required ---> check again and so on ...

3 个答案:

答案 0 :(得分:0)

在我看来,伪代码已经存在,只是不太清楚。 但看看这是否是你想要的:

private const decimal CurrentSuppliedPerBattery = 100;
private const decimal CurrentNeededPerBattery = 5;
private int BatteriesNeeded( List<A> As, List<B> Bs) {
    decimal currentToSupply = As.Sum( eachA => eachA.Current ) + Bs.Sum( eachB => eachB.Current );
    int batteries = 0;
    while(currentToSupply > 0)
    {
        int extraBatteries = Floor(1.0*currentToSupply/CurrentSuppliedPerBattery );
        batteries += extraBatteries;
        currentToSupply -= extraBatteries*CurrentSuppliedPerBattery;
        currentToSupply += extraBatteries*CurrentNeededPerBattery;
    }
    return batteries ;
}

ps:如果需要函数来处理列表,可以使用System.Linq,例如Sum()。

答案 1 :(得分:0)

问题不是很清楚(正如其他人在评论中所指出的那样),所以如果你能写一些更具体或具体的计算例子会很有用。无论如何,在我看来,你有一些带反馈的计算,你需要达到一个计算停止变化的点。

在数学中,可以使用fixed-point来描述。对于给定函数 f (您的计算),fixpoint是一个值,使得 x = f(x)(意味着如果再次重新计算该值,它将停止更改)。我不确定这是否可以帮助您实现,但在思考问题时,它绝对是一个有用的概念。

以下是计算给定函数的定点的方法示例(使用C#3.0 Func<T, T>委托)。该方法是通用的,需要能够比较值:

static T FixedPoint<T>(T initial, Func<T, T> calculateNext) 
    where T : IComparable<T> {
  T state = initial;
  T previous = default(T);
  do {
    previous = state;
    state = calculateNext(state);
  } while (previous.CompareTo(state) != 0);
  return state;
}

维基百科有一个计算 cos 函数定点的例子(参见右边的第二张图),您可以这样实现:

double val = FixedPoint(-1.0, f => Math.Cos(f));
Console.WriteLine(val);

这是描述一些循环的非常通用的方法,直到找到某个计算的稳定点为止。但是,你的问题不是很清楚,所以这可能不是你想要的......

答案 2 :(得分:0)

我会按照以下方式做点什么:

double CurrentFromEachBattery=100.0;
double CurrentNeededPerBattery=10.0;

int NumberOfBatteriesRequired(double activeCurrent, double passiveCurrent)
{
    int batteries=0;
    double currCurrent=0.0;
    double neededCurrent=activeCurrent+passiveCurrent;

    while( currCurrent < neededCurrent )
    {
        int newBatt = Math.Ceiling((neededCurrent - currCurrent) / CurrentFromEachBattery);
        neededCurrent += newBatt * CurrentNeededPerBattery;
        currCurrent += newBatt * CurrentFromEachBattery;
        batteries += newBatt;
    }

    return batteries;
}