== : B uden mellemrum
Applikation
import java.util.ArrayList;
public class App {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Bucket> Buckets=new ArrayList<Bucket>();
int[] tal = {100 , 50 , 25 , 50 , 25 , 30 , 10 , 70 , 10 , 30};
int boxSize = tal[0];
int count = 0;
for(int i = 1; i < tal.length; i++) {
boolean found=false;
for(Bucket bucket:Buckets){
if(found=bucket.addValue(tal[i])){
break;
}
}
if(!found){
Bucket b=new Bucket();
b.setMax(boxSize);
b.addValue(tal[i]);
Buckets.add(b);
}
}
for(Bucket bucket:Buckets){
bucket.printValues();
System.out.println();
}
}
}
Bucket klasse
import java.util.ArrayList;
public class Bucket{
private ArrayList<Integer> values;
private int Sum;
private int MaxSum;
public Bucket(){
values=new ArrayList<Integer>();
}
public void setMax(Integer value){
MaxSum=value;
}
public boolean addValue(Integer value){
if(Sum+value<=MaxSum){
values.add(value);
Sum=Sum+value;
return true;
}else{
return false;
}
}
public void printValues(){
for(Integer value : values) {
System.out.print(value+" ");
}
}
}
Indlæg senest redigeret d. 04.03.2008 22:38 af Bruger #5620