Re: Distance traveled (not traveling sales man)
On 4/4/2010 4:43 AM, anal_aviator wrote:
Hi,
I have a set number of distances (say 100)
They range between 1 and 50 miles, if my target is 100 miles and i need
to arrange them into groups what is the best way.
I.E
25,38,36
25,23,26,15,11
25,23,36,15
23,26,24,27
I could do a mix and mach for each distance against each other, but as the
data set grows it is going to get silly, and if possible I want to keep each
set to about the same size as the others. I.E 3-5 entries.
Or if someone could give me the name for the branch of maths that would
handle this, i can research that.
import java.util.ArrayList;
import java.util.List;
public class Scratch {
public static void main(String[] args) {
int[] data = new int[100];
for (int i = 0; i < 100; i++) {
data[i] = 1 + (int)(Math.random() * 50); }
List<List<Integer>> bins =
new ArrayList<List<Integer>>();
int count = 0;
List<Integer> currentBin;
while ( count < 100 ) {
double sub = 0;
currentBin = new ArrayList<Integer>();
bins.add(currentBin);
while (sub <= 100 && count < 100) {
if (sub + data[count] <= 100) {
sub += data[count];
currentBin.add(data[count]);
count++;
} else break;
}
}
for (List<Integer> l : bins) {
System.out.println(l.toString());
}
}
}