这个想法是一种回溯算法方法(带递归),您可以减少参数并得到局部解决方案,然后检查您是否有正确的解决方案.
public class Problem {
private static void algorithm(int n, int k, int m) {
algorithmRecursive(Collections.EMPTY_LIST, n, k, m, 1);
}
private static void algorithmRecursive(List partial, int n, int k, int m, int min) {
if ( (k > 0) ) {
// Optimization
if ((n <= k * m) && (n >= k*min)){
for (int i = min; i <= Math.min(m, n); i++) {
List newPartial = new ArrayList<>(partial);
newPartial.add(i);
algorithmRecursive(newPartial, n - i, k - 1, m, i);
}
}
} else if (n == 0) {
// Right solution
System.out.println(partial);
}
}
public static void main(String[] args) {
algorithm(7,4,3);
}
}