The previous proposed solutions all seem far too complicated. It is
not necessary to use a graph data structure. We can simply use an
array to store the quantities of the cups in a row, and update the
array to move to the next row. It would look something like this:
double cupQuan(double L, double C, int h, int i)
// h is the row number of the cup in question, the top cup has h = 1.
// i is the index of the cup in question; the first cup in a row has i
= 0.
{
int j, k;
double r, s;
double* p;
if( (L <= 0) || (C <= 0) || (h <= 0) || (i < 0) || (i >= h) )
return 0.0;
p = malloc(h * sizeof(double));
p[0] = L; // all liquid into top cup
for( j = 1 ; j < h ; ++j ) // advance from row j to j+1
{
r = 0.0; // nothing coming in from the
left
for( k = 0 ; k < j ; ++k )
{
s = p[k] - C;
if( s <= 0.0 )
{ // if this cup does not
overflow
p[k] = r; // only overflow from previous
cup
r = 0.0; // no overflow to next cup in
row
}
else
{ // if this cup does
overflow
p[k] = 0.5 * s + r; // overflow from this cup and
previous
r = 0.5 * s; // overflow to next cup in row
}
p[j] = r; // overflow into last cup in
row
}
}
s = p[i] <= C ? p[i] : C; // result, but not > C
free(p);
return s;
}
Dave
On Feb 25, 5:35 am, Ravi Ranjan <[email protected]> wrote:
> |_|
> |_| |_|
> |_| |_| |_|
> |_| |_| |_| |_|
> |_| |_| |_| |_| |_|
>
> Each cup has capacity C and once a cup gets full, it drops half extra
> amount to left child and half extra amount to right child
>
> for Eg : let' first cups get 2C amount of liquid then extra amount C(2C-C)
> will be divided equally to left and right child cup of next level
>
> i.e. C/2 to left child and C/2 to right child
>
> Write a function which takes input parameter as amount of liquid poured at
> top (L) and height of particular cup (h) index of that cup (i) and it
> should return amount of liquid absorbed in that cup.
>
> source
>
> http://www.careercup.com/question?id=12770661
>
> whats exactly the qestion???
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.