Estou tentando gerar todas as combinações possíveis de somas entre elementos de uma lista que resultem em um n estabelecido. Eu tentei os código abaixo mas não estou conseguindo limitar apenas aos itens da lista.
public static void main(String[] args) {
int n = 10;
int [ ] valores = {2,3,4};
for(int a = valores[0]; a <= n; a++) {
for(int b = valores[1]; b <= n; b++) {
for(int c = valores[2]; c <= n; c++) {
int soma = a+b+c;
if(soma==n)
System.out.printf("%d + %d + %d = %d \n",a,b,c, soma);
}
…
int n = 10;
List<Integer> lista = Arrays.asList(2,3,4,5);
for(int a = lista.get(0); a <= lista.get(3); a++) {
for(int b = lista.get(1); b <= lista.get(3); b++) {
for(int c = lista.get(2); c <= lista.get(3); c++) {
int soma = a+b+c;
if(soma==n)
System.out.printf("%d + %d + %d = %d \n",a,b,c, soma);
}
}
Neal
