我走到了尽头,需要您的帮助!! 我设法获得了给出特定总和的数字列表(双精度数)的组合。我也有一个哈希表,其值是前一个列表的数字,键是一些字符串,例如。数字(双精度)[2.45,2.65,4.67,5.25,2.45,2.65 ....]和表格[(E-40 = 2.45),(E-45 = 2.45),(E-56 = 2.65),(E -34 = 2.65),(E-24 = 4.67),(E-14 = 5.25)....]。因此,在对特定总和进行数字组合后,我得到:总和:5.10 ---> 2.45-2.65对应于(E-40,E-45)-(E-56,E-34)。我现在想获得这些集合的组合。
例如 [(E-40)-(E-56)],[(E-40)-(E-34)], [(E-45)-(E-56)],[(E-45)-(E-34)] 这是我的代码:`
public class TestTheCombinations {
public static void main(String[] args) {
ArrayList<Double> numbers = new ArrayList<>(Arrays.asList(2.45,2.45,2.65,2.65,4.67,5.25));
LinkedHashSet<Double> targets = new LinkedHashSet<Double>() {{
add(5.10);
}};
for (Double target: targets) {
Combinations combinations = new Combinations(numbers, target, false);
combinations.calculateCombinations();
for (String solution: combinations.getCombinations()) {
System.out.println(solution);
}
}
}
public static class Combinations {
private boolean allowRepetitions;
private int[] repetitions;
private ArrayList<Double> numbers;
private Hashtable<String,Double> table;
private Double target;
private Double sum;
private boolean hasNext;
private Set<String> combinations;
/**
* Constructor.
*
* @param numbers Numbers that can be used to calculate the sum.
* @param target Target value for sum.
*/
public Combinations(ArrayList<Double> numbers, Double target) {
this(numbers, target, true);
}
/**
* Constructor.
*
* @param numbers Numbers that can be used to calculate the sum.
* @param target Target value for sum.
*/
public Combinations(ArrayList<Double> numbers, Double target, boolean allowRepetitions) {
this.allowRepetitions = allowRepetitions;
if (this.allowRepetitions) {
Set<Double> numbersSet = new HashSet<>(numbers);
this.numbers = new ArrayList<>(numbersSet);
} else {
this.numbers = numbers;
}
this.numbers.removeAll(Arrays.asList(0));
Collections.sort(this.numbers);
this.target = target;
this.repetitions = new int[this.numbers.size()];
this.combinations = new LinkedHashSet<>();
this.sum = 0.0;
if (this.repetitions.length > 0)
this.hasNext = true;
else
this.hasNext = false;
}
/**
* Calculate and return the sum of the current combination.
*
* @return The sum.
*/
private Double calculateSum() {
this.sum = 0.0;
for (int i = 0; i < repetitions.length; ++i) {
this.sum += repetitions[i] * numbers.get(i);
}
return this.sum;
}
/**
* Redistribute picks when only one of each number is allowed in the sum.
*/
private void redistribute() {
for (int i = 1; i < this.repetitions.length; ++i) {
if (this.repetitions[i - 1] > 1) {
this.repetitions[i - 1] = 0;
this.repetitions[i] += 1;
}
}
if (this.repetitions[this.repetitions.length - 1] > 1)
this.repetitions[this.repetitions.length - 1] = 0;
}
/**
* Get the sum of the next combination. When 0 is returned, there's no other combinations to check.
*
* @return The sum.
*/
private Double next() {
if (this.hasNext && this.repetitions.length > 0) {
this.repetitions[0] += 1;
if (!this.allowRepetitions)
this.redistribute();
this.calculateSum();
for (int i = 0; i < this.repetitions.length && this.sum != 0; ++i) {
if (this.sum > this.target) {
this.repetitions[i] = 0;
if (i + 1 < this.repetitions.length) {
this.repetitions[i + 1] += 1;
if (!this.allowRepetitions)
this.redistribute();
}
this.calculateSum();
}
}
if (this.sum.compareTo(0.0) == 0.0)
this.hasNext = false;
}
return this.sum;
}
/**
* Calculate all combinations whose sum equals target.
*/
public void calculateCombinations() {
while (this.hasNext) {
if (this.next().compareTo(target) == 0)
this.combinations.add(this.toString());
}
}
/**
* Return all combinations whose sum equals target.
*
* @return Combinations as a set of strings.
*/
public Set<String> getCombinations() {
return this.combinations;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public String toString() {
Hashtable<String,Double> table = new Hashtable<String,Double>();
table.put("E-40",2.45);
table.put("E-45",2.45);
table.put("E-56",2.65);
table.put("E-34",2.65);
table.put("E-24",4.67);
table.put("E-14",5.25);
StringBuilder stringBuilder = new StringBuilder("For SUM " + sum + ": "+"\n");
StringBuilder stringBuilder2 = new StringBuilder();
for (int i = 0; i < repetitions.length; ++i) {
for (int j = 0; j < repetitions[i]; ++j) {
Set keys = new HashSet();
Double value = numbers.get(i);
for(Map.Entry entry: table.entrySet()){
if(value.equals(entry.getValue())){
keys.add(entry.getKey());
}
}
stringBuilder.append(numbers.get(i)+ " Corresponds WorkCode "+ keys+"\n");
}
}
return stringBuilder.toString() ;
}
}
}
`
感谢您的帮助!