Yalancı kod: yinelemeli toplam işlem

oy
1

Ben bir uygulamada bir mantığı yazmak için (bana en azından için zorlu) bir gereklilik verilmiştir. Ben aşağıdaki işlevselliği gerçekleştirmelidir burada bir iş mantığını yazmak için ettik

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

Şimdi akımı sağlamak için gerekli piller Diyelim (A + B) 'X' olmak

Ayrıca her bir X, dolayısıyla sadece batarya akımı tüketimi de dahil olmak üzere, ilk adım olarak, yine, toplam akım tüketimi hesaplamak gerekir, toplam akım tüketimi katkıda

yani

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

Şimdi yine ben gerekli pilleri hesaplanması gerekmektedir. Bize Y olarak bu diyelim

yani

Tedarik A'ya + B + X Biz pillerin Y numarası lazım.

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

Herkes pseudocode ilk set ile bana yardımcı olabilir? herhangi bir öneri de takdir

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 ...
Oluştur 04/03/2010 saat 19:32
kaynak kullanıcı
Diğer dillerde...                            


3 cevaplar

oy
0

Sözde kod, zaten orada sadece çok açık olmadığı gibi geliyor bana. Bu ne istiyorsun Ama eğer bakın:

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: Eğer Sum gibi (), listelerde üzerinde çalışmak için işlevler gerekiyorsa System.Linq kullanabilirsiniz.

Cevap 04/03/2010 saat 20:08
kaynak kullanıcı

oy
0

(Diğerleri açıklamalarda belirtildiği gibi) soru çok net değil, bu nedenle hesaplama biraz daha somut ya da belirli bir örnek yazabilirim eğer faydalı olacaktır. Neyse, bir geri besleme ile bazı hesaplama varsa ve hesaplama değişen durduran bir noktaya ulaşmak için gereken geliyor bana.

Matematik olarak, bu kullanılarak tanımlanabilir sabit nokta . Belirli bir işlev için f (sizin hesaplama) fixpoint bir değer şekildedir x = f (x) (Tekrar değerini yeniden hesaplamak bile, değişen durdurmak anlamına geliyor). Bu e-postanın uygulanması size yardımcı olabilir emin değilim, ama kesinlikle sorun hakkında düşünmeye kullanabileceğiniz faydalı bir kavramdır.

Burada, belirli bir fonksiyon (C # 3.0 kullanılarak bir sabit nokta hesaplayan bir yöntemin bir örneğidir Func<T, T>temsilci). Yöntem genel olduğu ve karşılaştırma yapabilmek olmalıdır:

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;
}

Ara bir sabit-noktasının hesaplanması, bir örnek vardır , çünkü böyle uygulayabilir fonksiyonu (sağdaki ikinci paragraf):

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

Bu bazı hesaplama istikrarlı noktasını bulana kadar çalışır bazı döngü tanımlamak için çok genel bir yoldur. Ancak, soru çok net değil, bu nedenle bu Aradığınız gibi olmayabilir ...

Cevap 04/03/2010 saat 20:10
kaynak kullanıcı

oy
0

Aşağıdaki çizgisinde bir şey yapacağını:

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;
}
Cevap 04/03/2010 saat 20:18
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more