Eingeschränkte permutationen

vichente

Mitglied
Hallo zusammen,
Ich möchte eine Methode schreiben die alle Mögliche Kombinationen von null bis zum Endzahl n erzeugt im aufsteigende Folge, und m Elemente soll jede Kombination erhalten.
Z.b.wenn das Endazhl n = 5 und m = 3 sollen Folgende Kombinationen ausgegeben werden:
012 024 124 234
013 025 125 235
014 034 134 245
015 045 135 345
023 123 145

Habe folgende Methode gebastelt,aber die funktioniert nicht richtig. Bin dankbar für jeden Rat und Hilfe.
Java:
  public static ArrayList<int[]> eingPerm(int m , int endZahl)
    {
        ArrayList<int[]> allKomb = new ArrayList<int[]>();
        
        for(int i = 0; i < m; i++){
            int[] temp = new int[m];
            for(int y = 0; y <= endZahl; y++){
                temp[i] = y;
            }
            allKomb.add(temp);
        }
        return allKomb;
    }
 

vichente

Mitglied
Habe versucht das Problem mit eine rekursive Methode zum lösen, leider funkioniert immer noch nicht, bitte um Rat und Hilfe.
Java:
import java.util.*;

public class Permutationen
{ 
    private ArrayList<int[]> allKomb = new ArrayList<int[]>();
    private int zahlenObergrenze;
    private int zahlenUntergrenze;
    private int anzahl;
 
    public Permutationen(int m, int o)
    {
        anzahl = m; 
        zahlenObergrenze = o;
        zahlenUntergrenze = 0;
    }
 
    public ArrayList<int[]> begPerm(int[] temp, int position)
    {
        ArrayList<int[]> kombis = new ArrayList<int[]>();
        
        if(anzahl == anzahl-1)
        {
            for(int i = temp[position-1]+1; i < position; i++){
                kombis.add(temp);
            }
        }
        if(position == 0)
        {
            for(int i = anzahl; i < zahlenObergrenze-anzahl+1; i++){
                kombis.addAll(begPerm(temp,position));
            } 
        }else{
           kombis.add(temp);
        }
        return allKomb;
    }
}
 

Ähnliche Java Themen


Oben