1

我被要求(至少对我来说具有挑战性)在应用程序中编写逻辑。我必须编写一个业务逻辑,其中它应该执行以下功能

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

现在假设提供电流(A+B) 所需的电池为“X”

每个 X 也可以贡献总电流消耗,因此我需要再次计算总电流消耗,就像第一步一样,包括电池电流消耗

IE

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

现在我应该再次计算所需的电池。让我们把它说成 Y

IE

要供应 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 ...
4

3 回答 3

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()。

于 2010-03-04T19:08:17.597 回答
0

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

在数学中,这可以使用定点来描述。对于给定的函数f(您的计算),固定点是一个满足x = f(x)的值(这意味着如果您再次重新计算该值,它将停止变化)。我不确定这是否可以帮助您实施,但它绝对是一个有用的概念,您可以在考虑问题时使用。

下面是计算给定函数的定点的方法示例(使用 C# 3.0Func<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;
}

Wikipedia 有一个计算cos函数不动点的示例(请参见右侧的第二张图),您可以这样实现:

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

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

于 2010-03-04T19:10:37.843 回答
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;
}
于 2010-03-04T19:18:59.037 回答