#1
04.12.2019 um 16:26 Uhr
leider nicht ganz richtig

Python :: Aufgabe #224 :: Lösung #2
cent = 100 d = [cent] lc = [10, 5, 2, 1] kombinationen = [] n = 0 def kombi(li,c): ko = [] for e in li: while e <= c: ko.append(e) c -= e return ko def xxx(d): global n global cent p = d[:] p.reverse() e = p[n] if e > 1: i = lc.index(e) + 1 p.remove(e) for j in range(2): p.append(lc[i]) if sum(p) < cent: p.append(1) sr(p) if p not in kombinationen: kombinationen.append(p) xxx(p) sr(p) if e == 1 : if n < len(p)-1: n += 1 xxx(p) def sr(s): s.sort(); s.reverse() # Start d = kombi(lc,cent) kombinationen.append(d) xxx(d) # Fehlende hinzufügen sr(kombinationen) for e in kombinationen: if len(e) > 1: p = e[:] if (p[-1] == 1 and p[-2] == 1) : del p[-1]; del p[-1] p.append(2) sr(p) if p not in kombinationen: if sum(p) == cent: kombinationen.append(p) sr(kombinationen) for e in kombinationen: if len(e) > 1: p = e[:] if (p[0] > p[1] and p[1] != 1 and p[2] == 1): del p[1] p.append(1); p.append(1) sr(p) if p not in kombinationen: if sum(p) == cent: kombinationen.append(p) sr(kombinationen) if kombinationen[-1][0] == 2: p = kombinationen[-1][:] del p[0] p.append(1); p.append(1) sr(p) kombinationen.append(p) # Doppelte ausfiltern k = [] for e in kombinationen: if e not in k: k.append(e) # Ausgabe sr(k) for e in k: print(e) print('Anzahl der Kombinationen:', len(k))
Kommentare:
AlexGroeg
Punkte: 2010
43 Lösungen
7 Kommentare
leider nicht ganz richtig