Advertisement
Guest User

Knapsack

a guest
Nov 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. from __future__ import division
  2. from __future__ import print_function
  3.  
  4. if __name__ == "__main__":
  5.     prices = [70, 85, 145, 260]
  6.     n = int(raw_input("Bitte geben Sie die Gesamtanzahl der Sendungen ein: "))
  7.     e = int(raw_input("Bitte geben Sie den Gesamtbetrag in Cent ein: "))
  8.     print()
  9.     solutions = []
  10.    
  11.     for w in range(0, n + 1):
  12.         print("Fortschritt: {0:d} %".format(int((w / (n + 1)) * 100)))
  13.        
  14.         for x in range(0, (n + 1) - w):
  15.            
  16.             for y in range(0, (n + 1) - (w + x)):
  17.                 z = n - (w + x + y)
  18.                
  19.                 if prices[0] * w + prices[1] * x + prices[2] * y + prices[3] * z == e:
  20.                     t = (w, x, y, z)
  21.                     solutions.append(t)
  22.    
  23.     print()
  24.     print("{:d} Loesungen gefunden.".format(len(solutions)))
  25.     print()
  26.    
  27.     for solution in solutions:
  28.        
  29.         for i in range(len(solution)):
  30.            
  31.             if i > 1:
  32.                 print(" + ", end = "")
  33.            
  34.             print("{:d} * {:d} ct. ".format(solution[i], prices[i]), end = "")
  35.        
  36.         print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement