Advertisement
tievo

Untitled

Apr 3rd, 2024 (edited)
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. billetes_usados = float("inf")
  2. dif = float('-inf')
  3.  
  4. c = 14
  5. B = [2,3,5,10,20,20]
  6.  
  7. def compare(a, b):
  8.     # La verdad no se explican muy bien los ordenes de prioridad de soluciones
  9.     # no se si se prioriza primero la cantidad de billetes o el monto a gastar
  10.     # aca priorizo la diferencia de monto a gastar y si hay empate, la cantidad de billetes
  11.  
  12.     if a[0] > b[0] or (a[0] == b[0] and a[1] < b[1]):
  13.         return a
  14.     return b
  15.  
  16. def bt(B, i, j, q):
  17.  
  18.     global billetes_usados
  19.     global dif
  20.    
  21.    
  22.     if i < 0 or j <= 0:
  23.         if j > 0: return (float('-inf'), float('inf'))
  24.         else: return (j, q)
  25.     else:
  26.         return compare(
  27.             bt(B, i-1, j, q),
  28.             bt(B, i-1, j-B[i], q+1)
  29.         )
  30.  
  31. def btPD(B, i, j, q):
  32.     global M
  33.  
  34.     if i < 0 or j <= 0:
  35.         if j > 0: return (float('-inf'), float('inf'))
  36.         else: return (j, q)
  37.  
  38.     if M[i][j] is None:
  39.         j_1, q_1 = btPD(B, i-1, j, q)
  40.         j_2, q_2 = btPD(B, i-1, j-B[i], q+1)
  41.  
  42.         M[i][j] = compare((j_1, q_1), (j_2, q_2))
  43.    
  44.     return M[i][j]
  45.    
  46. M = [[None for _ in range(c+1)] for _ in range(len(B)+1)]
  47.  
  48. print(btPD(B, len(B)-1, c, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement