can anyone explain how to write the following algorithm?

What is the algorithm for the following?

I have three locations ,,_
The first can have 0-2,
The second can have 0-4,
The third can have 0-3

I need to be able to generate the following list:

000
001
002
003
010
011
012
013
020
021
022
023
030
031
032
033
100
101
102
103
110
…and so on.

Can anyone explain how this can be done?

Seems remarkably similar to this one:

http://192.18.37.44/forums/index.php?topic=8617.0

In answer:


int[] location1 = new int[] {0,1,2};
int[] location2 = new int[] {0,1,2,3,4};
int[] location3 = new int[] {0,1,2,3};

for (int a=0;a<location1.length;a++) {
   for (int b=0;b<location2.length;b++) {
      for (int c=0;c<location3.length;c++) {
            System.out.println(location1[a]+""+location2[b]+""+location3[c]);
      }
   }
}

Might work…

Kev

Oops, thanks for the response kev, but I forgot to mention that it needs to work for n amount of locations.

Whats the link between the number of values available for each of the N entries? i.e. at the moment, why does location

1 = 0-2
2 = 0-4
3 = 0-3

If we had a 4th slow, what would the range be?

Kev

each location can have any range, there is no relation

0-2, 0-5, 0-3

or

0-1, 0-3, 0-8, 0-4

in my app I have an n amount of files. Each file has a certain amount of lines. I am trying to make all possible comparisons between all the lines. So if I have n = 3 files and each file breaks down like this:
file 1 = 2 lines
file 2 = 5 lines
file 3 = 3 lines

then all possible combos of 0-2, 0-5, 0-3 will be a complete check.

nevermind, figured it out. Thank you

If the answer is pretty simple, that would be nice to know, because atleast in my opinion it is nice to know how differently other people have solved problems.

I have done similar algorithm by adding n-amount of “NumberUnit”-classes (which can have only numbers 0-9 as their values) into an ArrayList. Of course this is far from efficient if the numbers go up to millions.

Sounds simply like 3 nested loops.

Also sounds like homework, which I dont do for people

Arise old thread, for I have need of you again.

Ok coming back with a trickier problem:

let say I have 4 elements {1,2,3,4} and I have 7 spaces. Now lets say that I have a certain amount of each element:
1 = 1
2 = 2
3 = 3
4 = 1

How would I get all the possible combinations of these counts into the 7 spaces?
ex:
combo 1 = 1223334
combo 2 = 1223343
combo 3 = 1223433
combo 4 = 1224333

Any help would be greatly appreciated

P.S. this is not homework.

Humm, after a fresh look in the morning, I realise that I can treat them as unqiue elements, generate all possible permutations of unqiue elements, and then get rid of the redundencies.

Really? Then whats the application?

fascinated