views:

179

answers:

1

Hello,

I'm going to complete my apprenticeship as coder and i got a nice j2me project to work on. But i have to admit that i'm not that good with mathematical algorithms as i'd like to be.

My problem is to create all possible "domino pairs" from a given set of values. For example: The possible values go from 0 to 6. Now imagine some domino tokens with those values. The output should then be something like this:

00
01
02
03
04
05
06
11
12
13
...

Each pair only appears a single time but pairs with two equal values are possible.

I've already searched for this problem, but either i didn't find a solution for this particular problem or i didn't really understand how the algorithms work.

I'd really appreciate any explanations and algorithms. Feel free to post alternative solutions as well. I'd prefer to not just have a solution, but to understand it as well ;)

Best wishes,
-- ceefar

P.S: Sorry, if some expressions may sound a bit awkward - english isn't my native language ;)

+7  A: 

Pseudo code:

for i from 0 to n inclusive
   for j from i to n inclusive
       output i,j

The important point is that the second loop doesn't start from zero. This means we don't have to test if a domino has already been used. We know that all dominos produced using this algorithm are unique because of the way the algorithm is constructed.

Mark Byers
Good one, you pipped me by a few minutes, with an essentially identical answer, so I've deleted mine.
Vatine
And i always thought, i had to use recursion ;) Thanks a lot!
ceefar