Java Reference
In-Depth Information
Write a function
static int
binaryToDecimal(
int
[] binaryRepresentation)
that
returns
the
integer
corresponding
to
the
binary
representation
of
binaryRepresentation
-
Here, we assume that
k
is big enough, and we are given the function:
public static int
[ ]
decimalToBinary(
int
n,
int
k)
{
int
[] binaryRepresentation =
new i n t
[k];
decimalToBinaryAux(n , 0 , binaryRepresentation ) ;
return
binaryRepresentation ;
}
Write the
recursive
function
decimalToBinaryAux
-
In the following, given a number
y
andaninteger
k
, chosen large
enough so that
y
can be written in base 2 using
k
bits, we denote by
y
[
i
]the
i
-th bit of the binary decomposition of
y
. That is, we have
k
2
i
.
y
=
y
[
i
]
∗
i
=0
We consider the following function called Grundy that takes as its
argument a number of fruits in each bin and returns an integer
Grundy
(
x
0
,x
2
,...,x
m−
1
)=
a
denoting the binary representation
defined by
a
[
i
]=
m−
1
x
l
[
i
]
mod 2.
l
=0
For example, if we want to compute
Grundy
(6
,
9
,
1
,
2), we first write
6, 9, 1 and 2 using base 2 (using the same number of
k
bits),
and we compute the sum on each column modulo 2. This yields a
representation in base 2 of
a
. We then convert
a
in base 10.
6=0110
9=1001
1=0001
2=0010
a
=1100
Thus we have
Grundy
(6
,
9
,
1
,
2) =
a
= 12.
-
Write function
static int
Grundy(
int
[] decimalTab)
that returns the
value of the Grundy function for the game configuration stored in
array
decimalTab
Search WWH ::
Custom Search